


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



CS 412/413 Spring 2007 Introduction to Compilers 1
CS 412/413 Spring 2007 Introduction to Compilers 2
CS 412/413 Spring 2007 Introduction to Compilers 3
CS 412/413 Spring 2007 Introduction to Compilers 4
S → if (E) S → if (E) if (E) S else S
S → if (E) S else S → if (E) if (E) S else S
S → if (E) S S → if (E) S else S S → other
if E 1 if E 2 S 1 S (^2) if E 1 if S (^2) E 2 S (^1)
CS 412/413 Spring 2007 Introduction to Compilers 5
statement → matched | unmatched matched → if (E) _______ else __________ | other unmatched → if (E) ________ | if (E) _______ else __________
CS 412/413 Spring 2007 Introduction to Compilers 6
statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) ________ | if (E) _______ else __________
CS 412/413 Spring 2007 Introduction to Compilers 7
statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) ________ | if (E) matched else unmatched
CS 412/413 Spring 2007 Introduction to Compilers 8
statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) statement | if (E) matched else unmatched
CS 412/413 Spring 2007 Introduction to Compilers 9
statement → if (E) matched optional-tail | other
matched → if (E) matched else matched | other
optional-tail → else tail | ε tail → if (E) tail | other
CS 412/413 Spring 2007 Introduction to Compilers 10
statement → if (E) matched optional-tail | other matched → if (E) matched else matched | other optional-tail → else tail | ε tail → if (E) tail | other
CS 412/413 Spring 2007 Introduction to Compilers 11
CS 412/413 Spring 2007 Introduction to Compilers 12
CS 412/413 Spring 2007 Introduction to Compilers 19
Expr parse_S() { switch (token) { case num: case ‘(’: Expr left = parse_E(); Expr right = parse_S’(); if (right == null) return left; else return new Add(left, right); default : throw new ParseError(); } }
S' → ε | +S E → num | (S)
CS 412/413 Spring 2007 Introduction to Compilers 20
CS 412/413 Spring 2007 Introduction to Compilers 21
void parse_S () { // parses sequence of E+E+E ... parse_E (); while (true) { switch (token) { case ‘+’: token = input.read(); parse_E(); break ; case ‘)’: case EOF: return ; default : throw new ParseError(); } }
CS 412/413 Spring 2007 Introduction to Compilers 22
Expr parse_S() { Expr result = parse_E(); while ( true ) { switch (token) { case ‘+’: token = input.read(); result = new Add(result, parse_E()); break ; case ‘)’: case EOF: return result; default : throw new ParseError(); } }
CS 412/413 Spring 2007 Introduction to Compilers 23
Language grammar
LL(1) grammar
predictive parsing table
recursive-descent parser
parser with AST generation
Left-recursion elimination Left-factoring
CS 412/413 Spring 2007 Introduction to Compilers 24
A → aACd | b C → c | ε
A → aACd | b C → A | ε
A → aAC | b C → c | ε