PL/0 Parser

143
PL/0 Parser COP 3402 System Software Fall 2013

description

PL/0 Parser. COP 3402 System Software Fall 2013. The Parsing Problem. Take a string of symbols in a language (tokens) and a grammar for that language to construct the parse tree or report that the sentence is syntactically incorrect . Two ways to do this: - PowerPoint PPT Presentation

Transcript of PL/0 Parser

Page 1: PL/0 Parser

PL/0 Parser

COP 3402 System SoftwareFall 2013

Page 2: PL/0 Parser

The Parsing Problem• Take a string of symbols in a language (tokens)

and a grammar for that language to construct the parse tree or report that the sentence is syntactically incorrect.

• Two ways to do this:– Top-Down (recursive descending parser).– Buttom-Up. (We don’t focus on this).

Page 3: PL/0 Parser

Top-Down Approach• Uses recursive procedures to model the parse

tree.• Beginning with the start symbol, for every

non-terminal (syntactic class) a procedure which parses that syntactic class is created.

Page 4: PL/0 Parser

PL/0 Grammar <program> ::= <block> . <block> ::= <const-decl> <var-decl> <proc-decl> <statement> <const-decl> ::= const <const-assignment-list> ; | e <const-assignment-list> ::= <ident> = <number> | <const-assignment-list> , <ident> = <number> <var-decl> ::= var <ident-list> ; | e <ident-list> ::= <ident> | <ident-list> , <ident> <proc-decl> ::= <proc-decl> procedure <ident> ; <block> ; | e <statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e <statement-list> ::= <statement> | <statement-list> ; <statement> <condition> ::= odd <expression> | <expression> <relation> <expression> <relation> ::= = | <> | < | > | <= | >= <expression> ::= <term> | <adding-operator> <term> | <expression> <adding-operator> <term> <adding-operator> ::= + | - <term> ::= <factor> | <term> <multiplying-operator> <factor> <multiplying-operator> ::= * | / <factor> ::= <ident> | <number> | ( <expression> )

Page 5: PL/0 Parser

PL/0 GrammarNon-Terminals

<program> <block> <const-decl> <var-decl> <proc-decl> <statement> <const-assignment-list> <ident> <number > <ident-list> <expression> <statement-list> <condition> <relation> <term> <adding-operator> <factor> <multiplying-operator>

Terminals

const, var, procedure, call, begin, end, if, then, while, do, odd

<> < > <= >= + - * / =

, ; e

We must implement a procedure for each one of this non-terminals.

Page 6: PL/0 Parser

In this parser we use:• TOKEN –a global variable that stores the current

token to analyze.• GET_TOKEN() – a procedure that takes the next

token in the string and stores it in TOKEN.• ENTER(type, name, params) – a procedure that

stores a new symbol into the Symbol Table.• ERROR() – a procedure that stops parsing, and shows

an error message.

Page 7: PL/0 Parser

<program> Procedure

procedure PROGRAM;begin

GET_TOKEN();BLOCK();if TOKEN <> "." then ERROR (No Period at end of file)

end;

<program> ::= <block> .

Page 8: PL/0 Parser

<block> Procedure

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

<block> ::= <const-decl> <var-decl> <proc-decl> <statement>

Page 9: PL/0 Parser

<const-decl> Procedure

procedure CONST-DECL;begin

repeatGET_TOKEN;if TOKEN <> IDENT then ERROR (missing identifier);GET_TOKEN;if TOKEN <> "=" then ERROR (identifier should be followed by =);GET_TOKEN;if TOKEN <> NUMBER then ERROR (= should be followed by number);ENTER(constant, ident, number);GET_TOKEN;

until TOKEN <> ",";if TOKEN <> ";" then ERROR (declaration must end with ;);GET_TOKEN;

end;

<const-decl> ::= const <const-assignment-list> ; | e<const-assignment-list> ::= <ident> = <number> | <const-assignment-list> , <ident> = <number>

Page 10: PL/0 Parser

<var-decl> Procedure

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR (missing identifier);GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR (declaration must end with ;);GET_TOKEN;

end;

<var-decl> ::= var <ident-list> ; | e<ident-list> ::= <ident> | <ident-list> , <ident>

Page 11: PL/0 Parser

<proc-decl> Procedure

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN;

if TOKEN <> IDENT then ERROR (missing procedure declaration);ENTER(procedure, ident);GET_TOKEN;if TOKEN <> ";" then ERROR (procedure declaration must end with ;);GET_TOKEN;BLOCK(level+1);if TOKEN <> ";" then ERROR (no ; at the end of block);GET_TOKEN;

end;end;

<proc-decl> ::= <proc-decl> procedure <ident> ; <block> ; | e

Page 12: PL/0 Parser

<statement> Procedure

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR (:= missing in statement);GET_TOKEN();EXPRESSION();

endelse if TOKEN = "call" then begin

GET_TOKEN();if TOKEN <> IDENT then ERROR (missing identifier);GET_TOKEN();

end…

<statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e<statement-list> ::= <statement> | <statement-list> ; <statement>

Page 13: PL/0 Parser

<statement> Procedure

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then ERROR (begin must be closed with end);GET_TOKEN();

end;…

<statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e<statement-list> ::= <statement> | <statement-list> ; <statement>

Page 14: PL/0 Parser

<statement> Procedure

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then ERROR (if condition must be followed by then);GET_TOKEN();STATEMENT();

end;…

<statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e<statement-list> ::= <statement> | <statement-list> ; <statement>

Page 15: PL/0 Parser

<statement> Procedure

procedure STATEMENT;…

else if TOKEN = "while" then beginGET_TOKEN();CONDITION();if TOKEN <> "do" then ERROR (while condition must be followed by do);GET_TOKEN();STATEMENT();

endend;

<statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e<statement-list> ::= <statement> | <statement-list> ; <statement>

Page 16: PL/0 Parser

<condition> Procedure

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then ERROR (relational operator missing in conditional

statement);GET_TOKEN();EXPRESSION();

endend;

<condition> ::= odd <expression> | <expression> <relation> <expression>

Page 17: PL/0 Parser

<expression> Procedure

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

<expression> ::= <term> | <adding-operator> <term> | <expression> <adding-operator> <term>

Page 18: PL/0 Parser

<term> Procedure

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do begin

GET_TOKEN();FACTOR();

endend;

<term> ::= <factor> | <term> <multiplying-operator> <factor>

Page 19: PL/0 Parser

<factor> Procedure

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR( left ( has not been closed );GET_TOKEN();

endelse ERROR (identifier, ( or number expected);

end;

<factor> ::= <ident> | <number> | ( <expression> )

Page 20: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

procedure PROGRAM;begin

GET_TOKEN();BLOCK();if TOKEN <> "." then ERROR (No

Period at end of file)end;

program()TOKEN=

Recursion stack

Symbol Table

Page 21: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

procedure PROGRAM;begin

GET_TOKEN();BLOCK();if TOKEN <> "." then ERROR (No

Period at end of file)end;

program()TOKEN= const

Recursion stack

Symbol Table

Page 22: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()

TOKEN= const

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

Recursion stack

Symbol Table

Page 23: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= const

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

Symbol Table

Page 24: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= m

Recursion stack

Symbol Table

Page 25: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= =

Recursion stack

Symbol Table

Page 26: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= 8

Recursion stack

Symbol Table

Page 27: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= 8

Recursion stack

m=8;

Symbol Table

Page 28: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= ;

Recursion stack

m=8;

Symbol Table

Page 29: PL/0 Parser

procedure CONST-DECL;begin

repeatGET_TOKEN();if TOKEN <> IDENT then

ERROR ();GET_TOKEN();if TOKEN <> "=" then ERROR

();GET_TOKEN();if TOKEN <> NUMBER then

ERROR);ENTER(constant, ident,

number);GET_TOKEN();

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()const-decl()

TOKEN= var

Recursion stack

m=8;

Symbol Table

Page 30: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()

TOKEN= var

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

Recursion stack

m=8;

Symbol Table

Page 31: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= var

Recursion stack

m=8;

Symbol Table

Page 32: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= a

Recursion stack

m=8;

Symbol Table

Page 33: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ,

Recursion stack

m=8;

Symbol Table

Page 34: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ,

Recursion stack

m=8; a;

Symbol Table

Page 35: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= b

Recursion stack

m=8; a;

Symbol Table

Page 36: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ,

Recursion stack

m=8; a;

Symbol Table

Page 37: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ,

Recursion stack

m=8; a; b;

Symbol Table

Page 38: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= c

Recursion stack

m=8; a; b;

Symbol Table

Page 39: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ;

Recursion stack

m=8; a; b;

Symbol Table

Page 40: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= ;

Recursion stack

m=8; a; b; c;

Symbol Table

Page 41: PL/0 Parser

procedure VAR-DECL;begin

repeatGET_TOKEN();

if TOKEN <> IDENT then ERROR ();

GET_TOKEN();ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()var-decl()

TOKEN= procedure

Recursion stack

m=8; a; b; c;

Symbol Table

Page 42: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()

TOKEN= procedure

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c;

Symbol Table

Page 43: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()proc-decl()

TOKEN= procedure

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c;

Symbol Table

Page 44: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()proc-decl()

TOKEN= ratio

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c;

Symbol Table

Page 45: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()proc-decl()

TOKEN= ratio

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 46: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()proc-decl()

TOKEN= ;

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 47: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block()proc-decl()

TOKEN= var

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 48: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)

TOKEN= var

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-

DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 49: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)

TOKEN= var

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-

DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 50: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= var

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 51: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= x

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 52: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= ,

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio;

Symbol Table

Page 53: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= ,

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio; x;

Symbol Table

Page 54: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= y

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio; x;

Symbol Table

Page 55: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= ;

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio; x;

Symbol Table

Page 56: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= ;

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 57: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)var-decl(2)

TOKEN= begin

procedure VAR-DECL;begin

repeatGET_TOKEN;

if TOKEN <> IDENT then ERROR ();

GET_TOKEN;ENTER(variable, ident, level);

until TOKEN <> ",";if TOKEN <> ";" then ERROR ();GET_TOKEN;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 58: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)

TOKEN= begin

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-

DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 59: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)

TOKEN= begin

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-

DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 60: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= begin

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 61: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= x

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 62: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= x

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 63: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= =

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 64: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= a

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 65: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= a

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 66: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= a

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 67: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= a

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR );GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 68: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= ;

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR );GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 69: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= ;

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 70: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= ;

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 71: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 72: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 73: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 74: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= y

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 75: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= y

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 76: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= =

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 77: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= b

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 78: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= b

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 79: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= b

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 80: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= b

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR );GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 81: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= ;

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR );GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 82: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= ;

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 83: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= ;

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 84: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR

();GET_TOKEN();EXPRESSION();

end…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 85: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 86: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 87: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= if

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 88: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= if

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then

ERROR ();GET_TOKEN();STATEMENT();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 89: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= b

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then

ERROR ();GET_TOKEN();STATEMENT();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 90: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)condition(2)

TOKEN= b

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then

ERROR ();GET_TOKEN();EXPRESSION();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 91: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)condition(2)

TOKEN= b

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then

ERROR ();GET_TOKEN();EXPRESSION();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 92: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)condition(2)

TOKEN= >

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then

ERROR ();GET_TOKEN();EXPRESSION();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 93: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)condition(2)

TOKEN= a

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then

ERROR ();GET_TOKEN();EXPRESSION();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 94: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)condition(2)

TOKEN= then

procedure CONDITION;begin

if TOKEN = "odd" then beginGET_TOKEN();EXPRESSION();

else beginEXPRESSION();if TOKEN <> RELATION then

ERROR ();GET_TOKEN();EXPRESSION();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 95: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= then

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then

ERROR ();GET_TOKEN();STATEMENT();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 96: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= begin

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then

ERROR ();GET_TOKEN();STATEMENT();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 97: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= begin

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 98: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= x

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 99: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 100: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= y

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 101: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 102: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 103: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 104: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 105: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 106: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "if" then beginGET_TOKEN();CONDITION();if TOKEN <> "then" then

ERROR ();GET_TOKEN();STATEMENT();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 107: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 108: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 109: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= c

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 110: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= c

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR);GET_TOKEN();EXPRESSION();

end

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 111: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= =

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR);GET_TOKEN();EXPRESSION();

end

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 112: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= x

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR);GET_TOKEN();EXPRESSION();

end

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 113: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= x

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 114: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= x

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 115: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= x

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR ();GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 116: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)factor(2)

TOKEN= /

procedure FACTOR;begin

if TOKEN = IDENTIFIER thenGET_TOKEN();

else if TOKEN = NUMBER thenGET_TOKEN();

else if TOKEN = "(" then beginGET_TOKEN();EXPRESSION();if TOKEN <> ")" then ERROR ();GET_TOKEN();

endelse ERROR ();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 117: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= /

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 118: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= y

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 119: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)term(2)

TOKEN= ;

procedure TERM;begin

FACTOR();while TOKEN = MULTIPLYING_OPERATOR do

beginGET_TOKEN();FACTOR();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 120: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)expression(2)

TOKEN= ;

procedure EXPRESSION;begin

if TOKEN = ADDING_OPERATOR then GET_TOKEN();

TERM();while TOKEN = ADDING_OPERATOR do begin

GET_TOKEN();TERM();

endend;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 121: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)statement(2)

TOKEN= ;

procedure STATEMENT;begin

if TOKEN = IDENT then beginGET_TOKEN();If TOKEN <> ":=" then ERROR);GET_TOKEN();EXPRESSION();

end

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 122: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 123: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 124: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 125: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 126: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

end;c = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)statement(2)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 127: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)block(2)

TOKEN= ;

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-

DECL();STATEMENT();

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 128: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)

TOKEN= ;

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 129: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)proc-decl(1)

TOKEN= begin

procedure PROC-DECL;begin while TOKEN = "procedure" do begin GET_TOKEN();

if TOKEN <> IDENT then ERROR ();ENTER(procedure, ident);GET_TOKEN();if TOKEN <> ";" then ERROR ();GET_TOKEN();BLOCK(level+1);if TOKEN <> ";" then ERROR ();GET_TOKEN();

end;end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 130: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)

TOKEN= begin

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 131: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= begin

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 132: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= a

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 133: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 134: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 135: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= b

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 136: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 137: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= call

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 138: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= ;

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 139: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 140: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= end

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 141: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)statement(1)

TOKEN= .

procedure STATEMENT;…

else if TOKEN = "begin" then beginGET TOKEN();STATEMENT();while TOKEN = ";" do begin

GET_TOKEN();STATEMENT();

end;if TOKEN <> "end" then

ERROR ();GET_TOKEN();

end;…

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 142: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

program()block(1)

TOKEN= .

procedure BLOCK;begin

if TOKEN = “const” then CONST-DECL();if TOKEN = “var” then VAR-DECL();if TOKEN = “procedure” then PROC-DECL();STATEMENT;

end;

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table

Page 143: PL/0 Parser

Small Exampleconst m = 8;var a, b, c;procedure ratio;var x, y;begin

x = a; y = b;if b > a then begin

x = b;y = a;

endc = x / y;

end;begin

a = m;b = 4;call ratio;

end.

procedure PROGRAM;begin

GET_TOKEN();BLOCK();if TOKEN <> "." then ERROR ()

end;

program()TOKEN= .

Recursion stack

m=8; a; b; c; ratio; x; y;

Symbol Table