COP 3402 Systems Software Euripides Montagne University of

  • Slides: 21
Download presentation
COP 3402 Systems Software Euripides Montagne University of Central Florida Eurípides Montagne University of

COP 3402 Systems Software Euripides Montagne University of Central Florida Eurípides Montagne University of Central Florida 1

COP 3402 Systems Software Intermediate Code Generation Eurípides Montagne University of Central Florida 2

COP 3402 Systems Software Intermediate Code Generation Eurípides Montagne University of Central Florida 2

Outline 1. From syntax graph to parsers 2. Tiny-PL/0 syntax 3. Intermediate code generation

Outline 1. From syntax graph to parsers 2. Tiny-PL/0 syntax 3. Intermediate code generation 4. Parsing and generating Pcode. Eurípides Montagne University of Central Florida 3

Building a parser from a Syntax Graph Transforming a grammar expressed in EBNF to

Building a parser from a Syntax Graph Transforming a grammar expressed in EBNF to syntax graph is advantageous to visualize the parsing process of a sentence because the syntax graph reflects the flow of control of the parser. Rules to construct a parser from a syntax graph (N. Wirth): B 1. - Reduce the system of graphs to as few individual graphs as possible by appropriate substitution. B 2. - Translate each graph into a procedure declaration according to the subsequent rules B 3 through B 7. B 3. - A sequence of elements S 1 S 2 Sm Is translated into the compound statement { T(S 1); T(S 2); …; T(Sn) } T(S) denotes the translation of graph S Eurípides Montagne University of Central Florida 4

Building a parser from a Syntax Graph Rules to construct a parser from a

Building a parser from a Syntax Graph Rules to construct a parser from a syntax graph: B 4. - A choice of elements S 1 S 2 Sn is translated into a selective or conditional statement Selective Conditional Switch (ch) { case ch in L 1 : T(S 1); case ch in L 2 : T(S 2); . . . if ch in L 1 {T(S 1) else if ch in L 2 { T(S 2) else. . . case ch in Ln : T(Sn); default: error } if ch in Ln { T(Sn)} else error If Li is a single symbol, say a, then “ch in Li” should be expressed as “ch == a” Eurípides Montagne University of Central Florida 5

Building a parser from a Syntax Graph Rules to construct a parser from a

Building a parser from a Syntax Graph Rules to construct a parser from a syntax graph: B 5. - A loop of the form S is translated into the statement while ch in L do T(S) where T(S) is the translation of S according to rules B 3 through B 7, and Li is a single symbol, say a, then “ch in Li” should be expressed as “ch == a”, however L could be a set of symbols. Eurípides Montagne University of Central Florida 6

Building a parser from a Syntax Graph Rules to construct a parser from a

Building a parser from a Syntax Graph Rules to construct a parser from a syntax graph: B 6. - A loop of the form a is translated into the statement if ch in L { T(S)} where T(S) is the translation of S according to rules B 3 through B 8, and Li is a single symbol, say a, then “ch in Li” should be expressed as “ch == a”, however L could be a set of symbols. Eurípides Montagne University of Central Florida 7

Building a parser from a Syntax Graph Rules to construct a parser from a

Building a parser from a Syntax Graph Rules to construct a parser from a syntax graph: B 7. - An element of the graph denoting another graph A A is translated into the procedure call statement A. B 8. - An element of the graph denoting a terminal symbol x x Is translated into the statement if (ch = x) { read(ch) } else {error } Where error is a routine called when an ill-formed construct is encountered. Eurípides Montagne University of Central Florida 8

Building a parser from a Syntax Graph Useful variants of rules B 4 and

Building a parser from a Syntax Graph Useful variants of rules B 4 and B 5: B 4 a. - A choice of elements x 1 S 1 x 2 S 2 xn Sn Eurípides Montagne Conditional if ch == ‘x 1’ { read(ch) T(S 1) } else if ch == ‘x 2‘ { read(ch) T(S 2) } else. . . if ch == ‘xn‘ { read(ch) T(Sn)} else error University of Central Florida 9

Building a parser from a Syntax Graph Useful variants of rules B 4 and

Building a parser from a Syntax Graph Useful variants of rules B 4 and B 5: B 5 a. - A loop of the form S x is translated into the statement while (ch == ‘x’ ) { read(ch); T(S); } Eurípides Montagne University of Central Florida 10

Example Applying the above mentioning rules to create one graph to this example: A

Example Applying the above mentioning rules to create one graph to this example: A : : = “x” | “(“ B “)” B : : = A C C : : = { “+” A } A ( B ) x B A C C A Eurípides Montagne University of Central Florida + 11

Syntax Graph We will obtain this graph: A ( A ) A + x

Syntax Graph We will obtain this graph: A ( A ) A + x Using this graph and choosing from rules B 1 to B 8 a parser program can be generated. Eurípides Montagne University of Central Florida 12

Parser program for the graph A (in PL/0) var ch: char; procedure A; begin

Parser program for the graph A (in PL/0) var ch: char; procedure A; begin if ch = ‘x’ then read(ch) else if ch = ‘(‘ then begin read(ch); A; while ch = ‘+’ do begin read(ch); A end; if ch = ‘)’ then read(ch) else error(err_number) end; begin read(ch); A end. Eurípides Montagne University of Central Florida 13

EBNF grammar for Tiny PL/0 (1) <program> : : = block ". ". <block>

EBNF grammar for Tiny PL/0 (1) <program> : : = block ". ". <block> : : = <const-declaration> <var-declaration> <statement> <constdeclaration> : : = [ “const” <ident> "=" <number> {", " <ident> "=" <number>} "; "] <var-declaration> : : = [ "var" <ident> {", " <ident>} "; "] <statement > : : = [<ident> ": =" <expression> | "begin" <statement> {"; " <statement> } "end" | "if" <condition> "then" <statement> |e] <condition> : : = "odd" <expression> | <expression> <rel-op> : : = "="|“<>"|"<="|">=“ <expression> : : = [ "+"|"-"] <term> { ("+"|"-") <term>} <term> : : = <factor> {("*"|"/") <factor>} <factor> : : = <ident> | <number> | "(" <expression> ")“ <number> : : = <digit> {<digit>} <Ident> : : = <letter> {<letter> | <digit>} <digit> ; ; = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9“ <letter> : : = "a" | "b" | … | "y" | "z" | "A" | "B" |. . . | "Y" | "Z" Eurípides Montagne University of Central Florida 14

Intermediate code generation cx code index Intermediate Code Generation code LOD 0 3 cx

Intermediate code generation cx code index Intermediate Code Generation code LOD 0 3 cx = 0 cx Each time an instruction is generated, It is stored in the code segment and the code index (cx) is incremented by one. Eurípides Montagne University of Central Florida 15

Parsing and generating pcode emit funtcion void emit(int op, int l, int m) {

Parsing and generating pcode emit funtcion void emit(int op, int l, int m) { if(cx > CODE_SIZE) error(25); else { code[cx]. op = op; //opcode[cx]. l = l; // lexicographical level code[cx]. m = m; // modifier cx++; } } Eurípides Montagne University of Central Florida 16

Parsing and generating pcode <expression> [+ | - ] <term> {( + | -

Parsing and generating pcode <expression> [+ | - ] <term> {( + | - ) <term>} void expression( ) { int addop; I f (token == plussym || token == minussym) { addop = token; get. Next. Token( ); term( ); if(addop == minussym) emit(OPR, 0, OPR_NEG); // negate } else term (); while (token == plussym || token == minussym) { addop = token; get. Next. Token( ); term(); if (addop == plussym) emit(OPR, 0, OPR_ADD); // addition else emit(OPR, 0, OPR_SUB); // subtraction } } Eurípides Montagne Function to parse an expression University of Central Florida 17

Parsing and generating pcode <term> <factor> { ( * | / ) <factor> }

Parsing and generating pcode <term> <factor> { ( * | / ) <factor> } void term( ) { int mulop; factor( ); while(token == multsym || token == slashsym) { mulop = token; get. Next. Token( ); factor( ); if(mulop == multsym) emit(OPR, 0, OPR_MUL); // multiplication else emit(OPR, 0, OPR_DIV); // division } } Eurípides Montagne University of Central Florida Parsing <term> 18

Parsing and generating pcode If <condition> then <statement> If (token == ifsym) { get.

Parsing and generating pcode If <condition> then <statement> If (token == ifsym) { get. Next. Token( ); condition( ); if(token != thensym) error(16); // then expected else get. Next. Token( ); ctemp = cx; gen(JPC, 0, 0); statement( ); code[ctemp]. m = cx; } Parsing the construct IF-THEN code JPC 0 0 ctemp statement cx changes JPC 0 0 to JPC 0 cx Eurípides Montagne University of Central Florida 19

Parsing and generating pcode while <condition> do <statement> If (token == whilesym) { cx

Parsing and generating pcode while <condition> do <statement> If (token == whilesym) { cx 1 =cx; get. Next. Token( ); condition( ); cx 2 = cx; gen(JPC, 0, 0) if(token != dosym) error(18); // then expected else get. Next. Token( ); statement( ); gen(JMP, 0, cx 1); code[cx 2]. m = cx; } Eurípides Montagne Parsing the construct WHILE-DO University of Central Florida code condition cx 1 JPC 0 cx cx 2 statement JMP 0 cx 1 cx 20

COP 3402 Systems Software The end Eurípides Montagne University of Central Florida 21

COP 3402 Systems Software The end Eurípides Montagne University of Central Florida 21