Textbook:Modern Compiler Design

Slides:



Advertisements
Similar presentations
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Advertisements

Compilation (Semester A, 2013/14) Lecture 6a: Syntax (Bottom–up parsing) Noam Rinetzky 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Recap Roman Manevich Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Mooly Sagiv and Roman Manevich School of Computer Science
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Context-Free Grammars Lecture 7
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial) 1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Introduction to Parsing
CSE 3302 Programming Languages
Chapter 3 – Describing Syntax
Parsing #1 Leonidas Fegaras.
A Simple Syntax-Directed Translator
Introduction to Parsing
CS 326 Programming Languages, Concepts and Implementation
G. Pullaiah College of Engineering and Technology
Programming Languages Translator
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Chapter 2 :: Programming Language Syntax
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Syntax Analysis Chapter 4.
Context-free Languages
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
Bison: Parser Generator
4 (c) parsing.
Syntax Analysis Sections :.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 2: A Simple One Pass Compiler
Subject Name:Sysytem Software Subject Code: 10SCS52
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Chapter 2 :: Programming Language Syntax
CSC 4181 Compiler Construction Context-Free Grammars
Kanat Bolazar February 16, 2010
Chapter 2 :: Programming Language Syntax
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Textbook:Modern Compiler Design Syntax Analysis Mooly Sagiv http://www.cs.tau.ac.il/~msagiv/courses/wcc04.html Textbook:Modern Compiler Design Chapter 2.2 (Partial) Explains the automaton first

A motivating example Create a desk calculator Challenges Non trivial syntax Recursive expressions (semantics) Operator precedence

Solution (lexical analysis) /* desk.l */ %% [0-9]+ { yylval = atoi(yytext); return NUM ;} “+” { return PLUS ;} “-” { return MINUS ;} “/” { return DIV ;} “*” { return MUL ;} “(“ { return LPAR ;} “)” { return RPAR ;} “//”.*[\n] ; /* comment */ [\t\n ] ; /* whitespace */ . { error (“illegal symbol”, yytext[0]); }

Solution (syntax analysis) /* desk.y */ %token NUM %left PLUS, MINUS %left MUL, DIV %token LPAR, RPAR %start p %% p : e {printf(“%d\n”, $1) ;} ; e : NUM { $$ = $1 ;} | LPAR E RPAR { $$ = $2; } | e PLUS e { $$ = $1 + $3 ; } | e MINUS e { $$ = $1 - $3 ; } | e MUL e { $$ = $1 * $3; } | e DIV e {$$ = $1 / $3; } #include “lex.yy.c” flex desk.l bison desk.y cc y.tab.c –ll -ly

Solution (syntax analysis) // input 7 + 5 * 3 a.out <input 22

Subjects The task of syntax analysis Automatic generation Error handling Context Free Grammars Ambiguous Grammars Top-Down vs. Bottom-Up parsing Bottom-up Parsing (next lesson)

Source program (string) Basic Compiler Phases Source program (string) Front-End lexical analysis Tokens syntax analysis Abstract syntax tree semantic analysis Back-End Fin. Assembly

Syntax Analysis (Parsing) input Sequence of tokens output Abstract Syntax Tree Report syntax errors unbalanced parenthesizes [Create “symbol-table” ] [Create pretty-printed version of the program] In some cases the tree need not be generated (one-pass compilers)

Handling Syntax Errors Report and locate the error Diagnose the error Correct the error Recover from the error in order to discover more errors without reporting too many “strange” errors

Example a := a * ( b + c * d ;

The Valid Prefix Property For every prefix tokens t1, t2, …, ti that the parser identifies as legal: there exists tokens ti+1, ti+2, …, tn such that t1, t2, …, tn is a syntactically valid program If every token is considered as single character: For every prefix word u that the parser identifies as legal: there exists w such that u.w is a valid program

Error Diagnosis Line number The current token The expected tokens may be far from the actual error The current token The expected tokens Parser configuration

Error Recovery Becomes less important in interactive environments Example heuristics: Search for a semi-column and ignore the statement Try to “replace” tokens for common errors Refrain from reporting 3 subsequent errors Globally optimal solutions For every input w, find a valid program w’ with a “minimal-distance” from w

Why use context free grammars for defining PL syntax? Captures program structure (hierarchy) Employ formal theory results Automatically create “efficient” parsers

Context Free Grammar (Review) What is a grammar Derivations and Parsing Trees Ambiguous grammars Resolving ambiguity

Context Free Grammars Non-terminals Terminals (tokens) Start non-terminal Terminals (tokens) Context Free Rules <Non-Terminal>  Symbol Symbol … Symbol

Example Context Free Grammar 1 S  S ; S 2 S  id := E 3 S  print (L) 4 E  id 5 E  num 6 E  E + E 7 E  (S, E) 8 L  E 9 L  L, E

Derivations Show that a sentence is in the grammar (valid program) Start with the start symbol Repeatedly replace one of the non-terminals by a right-hand side of a production Stop when the sentence contains terminals only Rightmost derivation Leftmost derivation

Example Derivations S S ; S 1 S  S ; S 2 S  id := E 3 S  print (L) 4 E  id 5 E  num 6 E  E + E 7 E  (S, E) 8 L  E 9 L  L, E S ; id := E id := E ; id := E id := num ; id := E id := num ; id := E + E id := num ; id := E + num id := num ; id :=num + num a 56 b 77 16

Parse Trees The trace of a derivation Every internal node is labeled by a non-terminal Each symbol is connected to the deriving non-terminal

Example Parse Tree S s S ; S s ; s S ; id := E id := E ; id := E id := id := num ; id := E id := num ; id := E + E id := E E + E id := num ; id := E + num num num num id := num ; id :=num + num

Ambiguous Grammars Two leftmost derivations Two rightmost derivations Two parse trees

A Grammar for Arithmetic Expressions 1 E  E + E 2 E  E * E 3 E  id 4 E  (E)

Drawbacks of Ambiguous Grammars Ambiguous semantics Parsing complexity May affect other phases

Non Ambiguous Grammar for Arithmetic Expressions 1 E  E + E 2 E  E * E 3 E  id 4 E  (E) E  E + T E  T T  T * F T  F 5 F  id 6 F  (E)

Non Ambiguous Grammars for Arithmetic Expressions 1 E  E + E 2 E  E * E 3 E  id 4 E  (E) E  E + T E  T T  T * F T  F 5 F  id 6 F  (E) E  E * T E  T T  F + T T  F 5 F  id 6 F  (E)

Efficient Parsers Pushdown automata Deterministic Report an error as soon as the input is not a prefix of a valid program Not usable for all context free grammars context free grammar bison “Ambiguity errors” parser tokens parse tree

context-free grammar design Designing a parser language design context-free grammar design Bison parser (c program)

Kinds of Parsers Top-Down (Predictive Parsing) LL Bottom-Up LR Construct parse tree in a top-down matter Find the leftmost derivation For every non-terminal and token predict the next production Preorder tree traversal Bottom-Up LR Construct parse tree in a bottom-up manner Find the rightmost derivation in a reverse order For every potential right hand side and token decide when a production is found Postorder tree traversal

Top-Down Parsing

Bottom-Up Parsing

Example Grammar for Predictive LL Top-Down Parsing expression  digit | ‘(‘ expression operator expression ‘)’ operator  ‘+’ | ‘*’ digit  ‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’

Parsing Expressions Try every alternative production For P  A1 A2 … An | B1 B2 … Bm If A1 succeeds Call A2 If A2 succeeds Call A3 If A2 fails report an error Otherwise try B1 Recursive descent parsing Can be applied for certain grammars Generalization: LL1 parsing

Predictive Parser for Arithmetic Expressions Grammar C-code? E  E + T E  T T  T * F T  F 5 F  id 6 F  (E)

Summary Context free grammars provide a natural way to define the syntax of programming languages Ambiguity may be resolved Predictive parsing is natural Good error messages Natural error recovery But not expressive enough But LR bottom-up parsing is more expressible