Skip to content

Commit

Permalink
Enable highlighting.
Browse files Browse the repository at this point in the history
  • Loading branch information
caballeto committed Jan 10, 2020
1 parent 3455061 commit 2a7dc6c
Show file tree
Hide file tree
Showing 58 changed files with 625 additions and 625 deletions.
26 changes: 13 additions & 13 deletions 01_Scanner/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We will start with a language that has only five lexical elements:
Each token that we scan is going to be stored in this structure
(from `defs.h`):

```
```c
// Token structure
struct token {
int token;
Expand All @@ -22,7 +22,7 @@ struct token {

where the `token` field can be one of these values (from `defs.h`):

```
```c
// Tokens
enum {
T_PLUS, T_MINUS, T_STAR, T_SLASH, T_INTLIT
Expand All @@ -41,7 +41,7 @@ ahead in the input stream. We also want to track what line we are currently
on so that we can print the line number in our debug messages. All of this
is done by the `next()` function:

```
```c
// Get the next character from the input file.
static int next(void) {
int c;
Expand All @@ -62,7 +62,7 @@ static int next(void) {
The `Putback` and `Line` variables are defined in `data.h` along with
our input file pointer:
```
```c
extern_ int Line;
extern_ int Putback;
extern_ FILE *Infile;
Expand All @@ -74,7 +74,7 @@ But `main.c` will remove the `extern_`; hence, these variables will

Finally, how do we put a character back into the input stream? Thus:

```
```c
// Put back an unwanted character
static void putback(int c) {
Putback = c;
Expand All @@ -86,7 +86,7 @@ static void putback(int c) {
We need a function that reads and silently skips whitespace characters
until it gets a non-whitespace character, and returns it. Thus:
```
```c
// Skip past input that we don't need to deal with,
// i.e. whitespace, newlines. Return the first
// character we do need to deal with.
Expand All @@ -107,7 +107,7 @@ So now we can read characters in while skipping whitespace; we can also
put back a character if we read one character too far ahead. We can
now write our first lexical scanner:

```
```c
// Scan and return the next token found in the input.
// Return 1 if token valid, 0 if no tokens left.
int scan(struct token *t) {
Expand Down Expand Up @@ -155,7 +155,7 @@ In fact, we already have to face this situation as we also need to
recognise integer literal values like `3827` and `87731`. Here is the
missing `default` code from the `switch` statement:
```
```c
default:
// If it's a digit, scan the
Expand All @@ -175,7 +175,7 @@ with this first character. It will return the scanned integer value. To
do this, it has to read each character in turn, check that it's a
legitimate digit, and build up the final number. Here is the code:

```
```c
// Scan and return an integer literal
// value from the input file. Store
// the value as a string in Text.
Expand Down Expand Up @@ -217,7 +217,7 @@ as well.
Here's the code for `chrpos()`:
```
```c
// Return the position of character c
// in string s, or -1 if c not found
static int chrpos(char *s, int c) {
Expand All @@ -235,7 +235,7 @@ And that's it for the lexical scanner code in `scan.c` for now.
The code in `main.c` puts the above scanner to work. The `main()`
function opens up a file and then scans it for tokens:

```
```c
void main(int argc, char *argv[]) {
...
init();
Expand All @@ -250,7 +250,7 @@ void main(int argc, char *argv[]) {
And `scanfile()` loops while there is a new token and prints out the
details of the token:
```
```c
// List of printable tokens
char *tokstr[] = { "+", "-", "*", "/", "intlit" };
Expand Down Expand Up @@ -317,7 +317,7 @@ Single character tokens are easy to scan, but multi-character tokens are
a bit harder. But at the end, the `scan()` function returns the next token
from the input file in a `struct token` variable:

```
```c
struct token {
int token;
int intvalue;
Expand Down
20 changes: 10 additions & 10 deletions 02_Parser/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Don't worry, I'll be here when you get back.
The structure of each node in the AST that we will build is described in
`defs.h`:

```
```c
// AST node types
enum {
A_ADD, A_SUBTRACT, A_MULTIPLY, A_DIVIDE, A_INTLIT
Expand Down Expand Up @@ -168,7 +168,7 @@ general function, `mkastnode()`, takes values for all four
fields in an AST node. It allocates the node, populates the field
values and returns a pointer to the node:

```
```c
// Build and return a generic AST node
struct ASTnode *mkastnode(int op, struct ASTnode *left,
struct ASTnode *right, int intvalue) {
Expand All @@ -192,7 +192,7 @@ struct ASTnode *mkastnode(int op, struct ASTnode *left,
Given this, we can write more specific functions that make a leaf AST
node (i.e. one with no children), and make an AST node with a single child:
```
```c
// Make an AST leaf node
struct ASTnode *mkastleaf(int op, int intvalue) {
return (mkastnode(op, NULL, NULL, intvalue));
Expand Down Expand Up @@ -238,7 +238,7 @@ separate. So, to start with, I'm going to have a function to map
the token values into AST node operation values. This, along with the
rest of the parser, is in `expr.c`:

```
```c
// Convert a token into an AST operation.
int arithop(int tok) {
switch (tok) {
Expand All @@ -264,7 +264,7 @@ syntax checking in our parser.
We need a function to check that the next token is an integer literal,
and to build an AST node to hold the literal value. Here it is:
```
```c
// Parse a primary factor and return an
// AST node representing it.
static struct ASTnode *primary(void) {
Expand All @@ -289,20 +289,20 @@ This assumes that there is a global variable `Token`, and that it
already has the most recent token scanned in from the input. In
`data.h`:

```
```c
extern_ struct token Token;
```

and in `main()`:

```
```c
scan(&Token); // Get the first token from the input
n = binexpr(); // Parse the expression in the file
```
Now we can write the code for the parser:
```
```c
// Return an AST tree whose root is a binary operator
struct ASTnode *binexpr(void) {
struct ASTnode *n, *left, *right;
Expand Down Expand Up @@ -404,7 +404,7 @@ interpretTree0(tree with +):

This is in `interp.c` and follows the above pseudo-code:

```
```c
// Given an AST, interpret the
// operators in it and return
// a final value.
Expand Down Expand Up @@ -444,7 +444,7 @@ sematic checking in our parser.
There is some other code here and the, like the call to the interpreter
in `main()`:
```
```c
scan(&Token); // Get the first token from the input
n = binexpr(); // Parse the expression in the file
printf("%d\n", interpretAST(n)); // Calculate the final result
Expand Down
12 changes: 6 additions & 6 deletions 03_Precedence/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ However, `additive_expr()` will have to defer to the higher-precedence

## `additive_expr()`

```
```c
// Return an AST tree whose root is a '+' or '-' binary operator
struct ASTnode *additive_expr(void) {
struct ASTnode *left, *right;
Expand Down Expand Up @@ -175,7 +175,7 @@ we would be combining sub-trees with multiple nodes in them.
## multiplicative_expr()
```
```c
// Return an AST tree whose root is a '*' or '/' binary operator
struct ASTnode *multiplicative_expr(void) {
struct ASTnode *left, *right;
Expand Down Expand Up @@ -247,7 +247,7 @@ for `expr2.c`. Let's start the tour.
Firstly, we need some code to determine the precedence levels for each
token:

```
```c
// Operator precedence for each token
static int OpPrec[] = { 0, 10, 10, 20, 20, 0 };
// EOF + - * / INTLIT
Expand Down Expand Up @@ -278,7 +278,7 @@ the `op_precedence()` function enforces the correct grammar syntax.
Now, instead of having a function for each precedence level, we have a
single expression function that uses the table of operator precedences:

```
```c
// Return an AST tree whose root is a binary operator.
// Parameter ptp is the previous token's precedence.
struct ASTnode *binexpr(int ptp) {
Expand Down Expand Up @@ -332,7 +332,7 @@ get the operator's token type, then loop building a tree.
The difference is the loop condition and body:
```
```c
multiplicative_expr():
while ((tokentype == T_STAR) || (tokentype == T_SLASH)) {
scan(&Token); right = primary();
Expand Down Expand Up @@ -361,7 +361,7 @@ to raise the operator precedence.

Once we hit a token at our precedence level or lower, we will simply:

```
```c
return (left);
```

Expand Down
Loading

0 comments on commit 2a7dc6c

Please sign in to comment.