Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.

Slides:



Advertisements
Similar presentations
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Advertisements

Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
Honors Compilers An Introduction to Grammars Feb 12th 2002.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Chapter 4 Lexical and Syntax Analysis Sections 1-4.
Fall 2007CS 2251 Miscellaneous Topics Deque Recursion and Grammars.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Environments and Evaluation
Chapter 2 A Simple Compiler
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Syntax and Semantics Structure of programming languages.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CPS120: Introduction to Computer Science Functions.
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
CS 536 Fall Scanner Construction  Given a single string, automata and regular expressions retuned a Boolean answer: a given string is/is not in.
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
Introduction to Yacc Ying-Hung Jiang
Chapter 3 Part II Describing Syntax and Semantics.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
Compilation With an emphasis on getting the job done quickly Copyright © – Curt Hill.
Introduction to Compiling
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
COMPILER CONSTRUCTION
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
User-Written Functions
Programming Languages Translator
Chapter 2 :: Programming Language Syntax
Using SLK and Flex++ Followed by a Demo
Automata and Languages What do these have in common?
PROGRAMMING LANGUAGES
Context-free Languages
Parser and Scanner Generation: An Introduction
Lexical and Syntax Analysis
Chapter 2 :: Programming Language Syntax
Chapter 2 :: Programming Language Syntax
Chapter 10: Compilers and Language Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill

Introduction Previously we used SLK and Flex++ to generate a Scanner –SLK found the terminals –We used regular expressions and C code to generate Flex++ input Now we have to finish the parser that uses it Copyright © 2015 Curt Hill

Scanner Review 1 Here are the steps we went through Start with a BNF file –Fed this into SLK Used the XXXConstants.h to identify terminal symbols Coded the definition section for Flex+ –Directive, includes and useful regular expressions Copyright © 2015 Curt Hill

Scanner Review 2 For each terminal in the language we made a Flex++ rule consisting of: –A regular expression –Some C++ code to process –Returned a token value Created a main program that executed the scanner –This could have been the third section Used make to execute Flex++ and then compile everything Copyright © 2015 Curt Hill

Original Scanner Modified The original scanner took a file as a command line parameter Each token was displayed with the token itself and the token number Next we modify this to feed a parser –Our first task is to take all that display stuff out –The scanner should just return values Copyright © 2015 Curt Hill

SLK Again When we fed the BNF into SLK it generated a number of parser routines –4.cpp These need to be augmented with three classes that we need to write: –XXXToken –XXXError –XXXAction –Where XXX was supplied by the –n= option of SLK Copyright © 2015 Curt Hill

XXXToken Class Next create the XXXToken class This is the interface between the parser and scanner It has two public methods: –int get() –int peek() or int peek()const; Both return a token but get consumes it and peek does not The peek function is used for the one token look ahead of LL(1) or LR(1) languages Copyright © 2015 Curt Hill

Look Ahead Copyright © 2015 Curt Hill factor constant ident ()expression When parsing a factor, we have three possible choices as to what comes next: A constant (which will be a numeric token) An identified (which is also a token) A left parenthesis (also a token) We use 1 token look ahed to determine what to do next An LR(3) or LL(3) language would require three token look ahead

XXXToken again This is the look ahead buffer Parser calls get when it knows it needs the next token It calls peek when it is deciding how to proceed –Peek returns the next token, but also leaves it there for the next get Copyright © 2015 Curt Hill

XXXError Class Error recovery is sometimes a sticky issue Do you: –Just quit there? –Attempt a recovery? –Generate an error message? The generalized parser cannot make these decisions, so leave it up to this class What methods are needed? Copyright © 2015 Curt Hill

XXXError Methods int mismatch (int terminal, int token); –Called if token does not match what is expected int no_entry (int nonterminal, int token, int level); –Called if the token is not among the known tokens void input_left (); –Called if there is input after parse thought it was complete void message(char *); –Called to display a message Copyright © 2015 Curt Hill

Beyond Parsing One of the possibilities of generating a parser is a simple recognizer The recognizer scans the file and gives us a simple answer: –File was correctly formed or not In general we want more: –Semantic analysis –Semantic content Copyright © 2015 Curt Hill

Semantic Analysis We want to know some things: –Was this variable defined before it was used? –Was this variable of the correct type for the expression it was in? These are things difficult or impossible to describe in BNF This is somewhere between syntax and semantics Copyright © 2015 Curt Hill

Semantic Content What does the program mean? What is it telling us? For a programming language we must now either: –Generate equivalent code as a compiler –Execute it as an interpreter In most SE applications we will do the latter This is why we need the action class Copyright © 2015 Curt Hill

XXXAction Class This class is used to communicate that the parser has figured out something That something is one of the following: –It has entered a new state –It is about to do a reduction –It wants to execute an action Each of these corresponds to a method that you need to define for the class Copyright © 2015 Curt Hill

State The method is: void state(int state); The state tells us what the parser is currently working on It is the least useful of the methods for us Copyright © 2015 Curt Hill

Reduction A reduction is when the parser has found everything on the RHS of a production and it is about to reduce it to the LHS of the production The signature for a reduce is: void reduce(int entry); The entry is a numbered production Copyright © 2015 Curt Hill

Reduction Processing The first thing, from a debugging perspective, that needs to be done is display the production This is done with the following function: char * XXXGetProductionName(int entry); This provides a copy of the production Copyright © 2015 Curt Hill

Augmented productions In the BNF that SLK accepts you may put actions to execute at the end of productions The format of the action is _action_X –The X is a number When the production is about to be reduced the execute function will get a call with this number Copyright © 2015 Curt Hill

Example Augmented Production Here are two productions or one with alternation: return_type ::= void _action_7 type _action_8 When the reduction void to type occurs you should be able to see the 7 It is almost that easy –As we will see Copyright © 2015 Curt Hill

Execute The signature is: void execute(int action_number); Every time one of the reductions is about to occur execute is called However, you always get a negative number and not the one you expect –If you add the value of the largest non- terminal constant to the received value it equal the action number Copyright © 2015 Curt Hill

Finally We generate a makefile to do everything but run it Next we do a demo Then an assignment Next lets consider the generated parser next Copyright © 2015 Curt Hill