CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 7 + 8 Ahmed Ezzat.

Slides:



Advertisements
Similar presentations
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Advertisements

Converting NFAs to DFAs. NFA to DFA: Approach In: NFA N Out: DFA D Method: Construct transition table Dtran (a.k.a. the "move function"). Each DFA state.
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Foundations of Software Design Lecture 24: Compilers, Lexers, and Parsers; Intro to Graphs Marti Hearst Fall 2002.
Lecture 2: Lexical Analysis CS 540 George Mason University.
Parser construction tools: YACC
C Chuen-Liang Chen, NTUCS&IE / 1 COMPILER Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
LEX and YACC work as a team
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
Introduction to Lex Ying-Hung Jiang
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction to Yacc Ying-Hung Jiang
Practical 1-LEX Implementation
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
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.
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.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Introduction to YACC CS 540 George Mason University.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Lecture 9 Symbol Table and Attributed Grammars
Syntax Analysis Part III
Lexical Analysis.
Tutorial On Lex & Yacc.
Compiler Construction
Compiler Lecture 1 CS510.
Syntax Analysis Part III
Bison: Parser Generator
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
Syntax Analysis Part III
Syntax Analysis Part III
Review: Compiler Phases:
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Syntax-Directed Translation Part I
Compiler Lecture Note, Miscellaneous
Syntax-Directed Translation Part I
Systems Programming & Operating Systems Unit – III
Presentation transcript:

CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat

CS 404Ahmed Ezzat 2 Lex Introduction A lexical analyzer generator A Unix utility Generates C code Takes regular expressions for patterns Takes additional C code Generates a combined NFA, then converts to DFA to recognize all patterns

CS 404Ahmed Ezzat 3 Using Lex Specifications Lex specifications is often put in a file *.l To compile, use  lex spec_file.l (creates lex.yy.c)  cc –o scan lex.yy.c –ll (compile and link with lex lib)  scan (run this program)

CS 404Ahmed Ezzat 4 Lex Input Specification File % { Declarations /* optional, copied to the C program */ %} Definitions /* optional, can give names to R.E.s */ % Rules /* required, regular expressions and actions */ % User subroutines /* optional, copied to the C program */

CS 404Ahmed Ezzat 5 Yacc Introduction A parser generator Stands for “yet another compiler compiler” Generate C code Use in conjunction with lex Use shift-reduce parsing algorithms (LALR)

CS 404Ahmed Ezzat 6 Using Yacc Specifications Lex specifications is often put in a file *.y To compile, use  yacc spec_file.y (creates y.tab.c, maybe y.tab.h)  gcc –o parse y.tab.o lex.yy.o –ll -ly(compile and link with libraries)  parse (run this program)

CS 404Ahmed Ezzat 7 Yacc Input Specification File % { Declarations /* optional, copied to the C program */ %} Definitions /* optional, can give names to R.E.s */ % Productions /* required, CFG rules */ % User subroutines /* optional, copied to the C program */

CS 404Ahmed Ezzat 8 Parser-Lexer Communications Define the same set of constant in lex and yacc programs (same tokens, can be defined in either place) – For example, parser program defines token IDENTIFIER and lexer include “y.tab.h” They may share the same symbol table

CS 404Ahmed Ezzat 9 Global Variables yytext – the text that just matched a pattern Yyleng – the length of the lexeme stored in yytext yylval – value associated with a token, returned from lexer to parser. It is of UNION type: – E.g., yylval.token = 100 – E.g., yylcal.string = yytext yylloc – the location (line and column) of a token

CS 404Ahmed Ezzat 10 Global Functions yylex() – The lexer C routine produced by lex – May return the token just found – May be called by the parser yyparse() – The parser function – Returns 0 if successful, 1 if failed

CS 404Ahmed Ezzat 11 Syntax Directed Translation Perform analysis and translation while parsing – Generate intermediate code – Save information in the symbol table – Issue error message May use a separate pass, but parse trees are often big and traverse it takes time

CS 404Ahmed Ezzat 12 How to Perform Syntax Directed Parsing Associate “attributes” to symbols in CFG One symbol may have many attributes – Integer value, e.g, 10 – String value, e.g, “island” – Variable type, e.g., int – Function return type, e.g., boolean – Function number of args Compute attribute value from existing values using “semantic rules”

CS 404Ahmed Ezzat 13 Synthesized and Inherited Attributes Synthesized attribute: the value is computed from the values of the children – E.g., values of terminals – E.g., values of arithmetic expressions Inherited attribute: the value is computed from the values of the siblings or parent – E.g., type

CS 404Ahmed Ezzat 14 More Concepts A side effect is a change that cannot be reversed – E.g., printing a value – E.g., updating a global variable A syntax-directed definition is a CFG with semantic rules An attribute grammar is a syntax-directed definition in which functions in semantic rules has no side effect An annotated parse tree is a parse tree with the values of the attributes

CS 404Ahmed Ezzat 15 How to Evaluate the Value of an Attribute Need to traverse the tree in the order that preserves dependency For synthesized attributes only, may use depth first traversals For more complex dependencies, use dependency graph (DAG: directed acyclic graph)