1 Lex. 2 Lex is a lexical analyzer Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Chapter 2-2 A Simple One-Pass Compiler
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Courtesy Costas Busch - RPI1 Positive Properties of Context-Free languages.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 More Applications of the Pumping Lemma. 2 The Pumping Lemma: Given a infinite regular language there exists an integer for any string with length we.
Costas Busch - RPI1 Positive Properties of Context-Free languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
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.
1 More Applications of The Pumping Lemma. 2 The Pumping Lemma: there exists an integer such that for any string we can write For infinite context-free.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
1 YACC Yet Another Compiler Compiler. 2 Yacc is a parser generator: Input: A Grammar Output: A parser for the grammar (Reminder: a parser finds derivations)
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Fall 2006Costas Busch - RPI1 Lex. Fall 2006Costas Busch - RPI2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found.
Costas Busch - RPI1 More Applications of the Pumping Lemma.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
贺天行.  First please download to appetitzer in our webpage  This tutorial will be mainly about codes provided in the appetizer.
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.
CSC 338: Compiler design and implementation
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.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
CPS 506 Comparative Programming Languages Syntax Specification.
Prof Busch - LSU1 YACC Yet Another Compiler Compiler.
Simple One-Pass Compiler
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Syntax Analyzer (Parser)
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Costas Busch - LSU1 Lex. Costas Busch - LSU2 Lex: a lexical analyzer A Lex program recognizes strings For each kind of string found the lex program takes.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
CS 3304 Comparative Languages
More Applications of the Pumping Lemma
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Simplifications of Context-Free Grammars
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
Compilers B V Sai Aravind (11CS10008).
R.Rajkumar Asst.Professor CSE
Parsing Costas Busch - LSU.
Predictive Parsing Program
Review for the Midterm. Overview (Chapter 1):
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

1 Lex

2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn: ; Keyword: if Paren: ( Ident: test Oper: >.... Input Output

3 For each kind of strings there is a regular expression “if” “then” “+” “-” “=“ /* operators */ /* keywords */ Lex Regular expressions

4 (0|1|2|3|4|5|6|7|8|9)+ /* integers */ /* identifiers */ Lex Regular expressions (a|b|..|z|A|B|...|Z)+

5 integers [0-9]+(0|1|2|3|4|5|6|7|8|9)+

6 (a|b|..|z|A|B|...|Z)+ [a-zA-Z]+ identifiers

7 Each regular expression has an action: Examples: \n Regular expressionAction linenum++ [a-zA-Z]+ printf(“identifier”); [0-9]+ prinf(“integer”);

8 Default action: ECHO; Print the string identified to the output

9 A small program % [a-zA-Z]+printf(“Identifier\n”); [0-9]+prinf(“Integer\n”); [ \t\n] ; /*skip spaces*/

test var Input Output Integer Identifier Integer

11 % [a-zA-Z]+ printf(“Identifier\n”); [0-9]+ prinf(“Integer\n”); [ \t] ; /*skip spaces*/. printf(“Error in line: %d\n”, linenum); Another program %{ int linenum = 1; %} \nlinenum++;

test var temp Input Output Integer Identifier Integer Error in line 3 Identifier

13 Lex matches the longest input string “if” “ifend” Regular Expressions Input: ifend if ifn Matches: “ifend” “if” nomatch

14 Internal Structure of Lex Lex Regular expressions NFADFA Minimal DFA The final states of the DFA are associated with actions

15 Compilers

16 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code

17 Lexical analyzer parser Compiler program machine code

18 Parser knows the grammar of the programming language

19 Parser PROGRAM -> STMT_LIST STMT_LIST -> STMT STMT_LIST | STMT; STMT -> EXPR ; | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR -> EXPR + EXPR | EXPR - EXPR | ID IF_STMT -> if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT-> while (EXPR) do STMT

20 The parser constructs the derivation for the particular input program * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => * E => * 5 input derivation

21 10 E 25 E => E + E => E + E * E => 10 + E*E => * E => * 5 derivation derivation tree EE EE + *

22 10 E 25 derivation tree EE EE + * mult t1, 10, 5 add t2, 10, t1 machine code

23 Parsing

24 grammar Parser input string derivation

25 Example: Parser derivation input ?

26 Exhaustive Search Phase 1:

27

28 Phase 2 Phase 1

29 Phase 2 Phase 1

30 Phase 2 Phase 3

31 Final result of exhaustive search Parser derivation input (Top-down parsing)

32 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :

33 Time for phase 1: possible derivations For grammar with rules

34 Time for phase 2: possible derivations

35 Time for phase : possible derivations

36 Total time needed for string : Extremely bad!!!

37 There exist faster algorithms for specialized grammars S-grammar: symbol string of variables appears once

38 S-grammar example: Each string has a unique derivation

39 In the exhaustive search parsing there is only one choice in each phase For S-grammars: Total time for parsing string : Time for a phase: 1

40 For general context-free grammars: There exists a parsing algorithm that parses a string in time