Next: Example run of yacc Up: CS2111: Design and Implementation Previous: A small example -   Contents

# Example run of lex calculator

The example will recognise the input line: 12 3 +14 2-*;newline
^ indicates where we are on the line, and newline means the single character often represented by `\`n.

```
^12 3 +14 2-*;newline
stack= (empty)

12^ 3 +14 2-*;newline
rule= [0-9]+
stack= 12

12 ^3 +14 2-*;newline
rule= [ \t\n]
stack= 12
```

although the space is discarded, it is essential to separate the two numbers, otherwise they would be read as one long number 123

```
12 3^ +14 2-*;newline
rule= [0-9]+
stack= 12 3

12 3 ^+14 2-*;newline
rule= [ \t\n]
stack= 12 3
```

this space is not essential to separate 3 and + as they are not recognised by the same pattern in the example calculator

```
12 3 +^14 2-*;newline
rule= "+"
stack= 15

12 3 +14^ 2-*;newline
rule= [0-9]+
stack= 15 14

12 3 +14 ^2-*;newline
rule= [ \t\n]
stack= 15 14

12 3 +14 2^-*;newline
rule= [0-9]+
stack= 15 14 2

12 3 +14 2-^*;newline
rule= -
stack= 15 12

12 3 +14 2-*^;newline
rule= "*"
stack= 180

12 3 +14 2-*;^newline
rule= ;
stack= (empty)
print "180\n"

12 3 +14 2-*;newline^
rule= [ \t\n]
stack= (empty)
```

Pete Jinks
1999-09-30