Designing a Predictive Parser

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Lexical Analysis Recognize tokens and ignore white spaces, comments
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Simple One-Pass Compiler
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 Semantic Analysis  Semantic analysis includes  Dynamic Checking (Those checks for which to perform, compiler doesn’t have sufficient information) 
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Chapter 2: A Simple One Pass Compiler
CS 3304 Comparative Languages
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Programming Languages Translator
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Textbook:Modern Compiler Design
Syntax Specification and Analysis
Table-driven parsing Parsing performed by a finite state machine.
PROGRAMMING LANGUAGES
Parsing with Context Free Grammars
CS 363 Comparative Programming Languages
4 (c) parsing.
Syntax Analysis Sections :.
Parsing Techniques.
CSE 3302 Programming Languages
Syntax Analysis Sections :.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Lexical and Syntactic Analysis
CPSC 388 – Compiler Design and Construction
Syntax-Directed Definition
Chapter 3: Lexical Analysis
Chapter 2: A Simple One Pass Compiler
Lexical Analysis - An Introduction
CSE 3302 Programming Languages
CSE401 Introduction to Compiler Construction
Chapter 2: A Simple One Pass Compiler
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Parsing Costas Busch - LSU.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
CS 3304 Comparative Languages
Semantic Analysis Semantic analysis includes
Computing Follow(A) : All Non-Terminals
Chapter 4: Lexical and Syntax Analysis Sangho Ha
SYNTAX DIRECTED DEFINITION
Lexical Analysis - An Introduction
BNF 9-Apr-19.
Compiler Construction
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Faculty of Computer Science and Information System
Presentation transcript:

Designing a Predictive Parser Consider A FIRST()=set of leftmost tokens that appear in  or in strings generated by . E.g. FIRST(type)={,array,integer,char,num} Consider productions of the form A, A the sets FIRST() and FIRST() should be disjoint Then we can implement predictive parsing (initially: start NT + lookahead=lefmost) Starting with A? we find into which FIRST() set the lookahead symbol belongs to and we use this production. Any non-terminal results in the corresponding procedure call Terminals are matched.

Problems with Top Down Parsing Left Recursion in CFG May Cause Parser to Loop Forever. Indeed: In the production AA we write the program procedure A { if lookahead belongs to First(A) then call the procedure A } Solution: Remove Left Recursion... without changing the Language defined by the Grammar.

Dealing with Left recursion Solution: Algorithm to Remove Left Recursion: BASIC IDEA: AA| becomes A R R R|  expr  expr + term | expr - term | term term  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr  term rest rest  + term rest | - term rest | 

What happens to semantic actions? expr  expr + term {print(‘+’)}  expr - term {print(‘-’)}  term term  0 {print(‘0’)} term  1 {print(‘1’)} … term  9 {print(‘9’)} expr  term rest rest  + term {print(‘+’)} rest  - term {print(‘-’)} rest   term  0 {print(‘0’)} term  1 {print(‘1’)} … term  9 {print(‘9’)}

Comparing Grammars with Left Recursion Notice Location of Semantic Actions in Tree What is Order of Processing? expr term {print(‘2’)} {print(‘+’)} {print(‘5’)} {print(‘-’)} {print(‘9’)} 5 + 2 - 9

Comparing Grammars without Left Recursion Now, Notice Location of Semantic Actions in Tree for Revised Grammar What is Order of Processing in this Case? {print(‘2’)} expr term term {print(‘-’)} term {print(‘+’)} {print(‘5’)} {print(‘9’)} rest 2 5 - 9 +  rest

The Lexical Analysis Process A Graphical Depiction returns token to caller uses getchar ( ) to read character lexan ( ) lexical analyzer pushes back c using ungetc (c , stdin) tokenval Sets global variable to attribute value

The Lexical Analysis Process Functional Responsibilities Input Token String Is Broken Down White Space and Comments Are Filtered Out Individual Tokens With Associated Values Are Identified Symbol Table Is Initialized and Entries Are Constructed for Each “Appropriate” Token Under What Conditions will a Character be Pushed Back?

Example of a Lexical Analyzer function lexan: integer ; var lexbuf : array[ 0 .. 100 ] of char ; c : char ; begin loop begin read a character into c ; if c is a blank or a tab then do nothing else if c is a newline then lineno : = lineno + 1 else if c is a digit then begin set tokenval to the value of this and following digits ; return NUM end

Algorithm for Lexical Analyzer else if c is a letter then begin place c and successive letters and digits into lexbuf ; p : = lookup ( lexbuf ) ; if p = 0 then p : = insert ( lexbf, ID) ; tokenval : = p return the token field of table entry p end else set tokenval to NONE ; / * there is no attribute * / return integer encoding of character c Note: Insert / Lookup operations occur against the Symbol Table !

Symbol Table Considerations OPERATIONS: Insert (string, token_ID) Lookup (string) NOTICE: Reserved words are placed into symbol table for easy lookup Attributes may be associated with each entry, i.e., Semantic Actions Typing Info: id  integer etc. ARRAY symtable lexptr token attributes div mod id 1 2 3 4 d i v EOS m o d EOS c o u n t EOS i EOS ARRAY lexemes

A Brief Look at Code Generation Back-end of Compilation Process - Which Will Not Be Our Emphasis We’ll Focus on Front-end Important Concepts to Re-emphasize •• Abstract Stack Machine for Intermediate Code Generation: (i) basic arithmetic, (ii) stack, (iii), flow control •• L-value Vs. R-value of an identifier I : = 5 ; L - Location I : = I + 1 ; R - Contents

A Brief Look at Code Generation Employ Statement Templates for Code Generation. Each Template Characterizes the Translation Different Templates for Each Major Programming Language Construct, if, while, procedure, etc. WHILE IF label test code for expr code for expr gofalse out gofalse out code for stmt code for stmt label out goto test label out

Concluding Remarks / Looking Ahead We’ve Reviewed / Highlighted Entire Compilation Process Introduced Context-free Grammars (CFG) and Indicated /Illustrated Relationship to Compiler Theory Reviewed Many Different Versions of Parse Trees That Assist in Both Recognition and Translation We’ll Return to Beginning - Lexical Analysis We’ll Explore Close Relationship of Lexical Analysis to Regular Expressions, Grammars, and Finite Automatons