



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These lecture notes cover topics such as quizzes, parsing, semantic analysis, and recursive descent parsing in the context of a computer science course (cpsc 326) during the spring 2013 semester. The notes include examples, exercises, and explanations of various concepts related to parsing and semantic analysis.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Today ...
Simple list of assignment statements
stmt list → stmt | stmt ‘;’ stmt list
stmt → var ‘=’ expr
var → ‘A’ | ‘B’ | ‘C’
expr → var | var ‘+’ var | var ‘-’ var
Issues with our example (for LL(k))
Q: Is it left-recursive? Nope (we’re ok)
Q: Can it be left factored? Yes (need to fix)
Q: Is it ambiguous? Nope (we’re ok)
Q: How many look ahead tokens needed? 6 (need to fix)
Q: How should we fix it?
A simple approach to ad hoc parsing
Example program → stmt list $$ stmt list → var ‘=’ expr stmt list tail void advance() { // helper function tok = scanner.next_token(); } void eat(int t) { // helper function if(t == tok.type()) advance(); else error(); } void program() { // program lhs advance(); // get the first token stmt_list(); // do stmt list rule eat(END); // expecting $$ } void stmt_list() { // stmt_list lhs if(tok.type != END) { var(); // do var rule assign(); // do assign rule expr(); // do expr rule stmt_list_tail(); // do statement list tail } }
As we parse, we can build the AST
Adding AST creation to Recursive Descent example
MyProgram* program() { advance(); p = new MyProgram(); stmt_list(p); eat(END); return p; }
void stmtList(MyProgram* p) { if(tok.type != END) { Variable* v = var(); Expression* e = expr(); Statement* s = new Statement(v, e); p->addStatement(s); stmt_list_tail(p); } }