Pembangunan Kompilator.  A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence of that language, and.

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

Finite Automata CPSC 388 Ellen Walker Hiram College.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 2 Mälardalen University 2005.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
1 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan et al. Lecture-Module 5 More Lexical Analysis.
Lexical Analysis - Scanner Computer Science Rensselaer Polytechnic Compiler Design Lecture 2.
Lecture # 5. Topics Minimization of DFA Examples What are the Important states of NFA? How to convert a Regular Expression directly into a DFA ?
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
1 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's.
1 Pertemuan Lexical Analysis (Scanning) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
Scanner Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the.
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
Topic #3: Lexical Analysis
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions.
Chapter 3 Chang Chi-Chung The Role of the Lexical Analyzer Lexical Analyzer Parser Source Program Token Symbol Table getNextToken error.
CS308 Compiler Principles Lexical Analyzer Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Fall 2012.
Overview of Previous Lesson(s) Over View  Strategies that have been used to implement and optimize pattern matchers constructed from regular expressions.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
Lexical Analysis Constructing a Scanner from Regular Expressions.
Lexical Analyzer (Checker)
Overview of Previous Lesson(s) Over View  An NFA accepts a string if the symbols of the string specify a path from the start to an accepting state.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
Chapter 3 Chang Chi-Chung The Role of the Lexical Analyzer Lexical Analyzer Parser Source Program Token Symbol Table getNextToken error.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
1 Lexical Analysis and Lexical Analyzer Generators Chapter 3 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
By Neng-Fa Zhou Lexical Analysis 4 Why separate lexical and syntax analyses? –simpler design –efficiency –portability.
Lecture # 4 Chapter 1 (Left over Topics) Chapter 3 (continue)
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
Fall 2003CS416 Compiler Design1 Lexical Analyzer Lexical Analyzer reads the source program character by character to produce tokens. Normally a lexical.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
Overview of Previous Lesson(s) Over View  Algorithm for converting RE to an NFA.  The algorithm is syntax- directed, it works recursively up the parse.
Lexical Analyzer CS308 Compiler Theory1. 2 Lexical Analyzer Lexical Analyzer reads the source program character by character to produce tokens. Normally.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
June 13, 2016 Prof. Abdelaziz Khamis 1 Chapter 2 Scanning – Part 2.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
Compilers Lexical Analysis 1. while (y < z) { int x = a + b; y += x; } 2.
COMP 3438 – Part II - Lecture 3 Lexical Analysis II Par III: Finite Automata Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Finite-State Machines (FSMs)
Lexical analysis Finite Automata
Finite-State Machines (FSMs)
Chapter 2 Finite Automata
Introduction to Lexical Analysis
The time complexity for e-closure(T).
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
Lexical Analysis and Lexical Analyzer Generators
Recognition of Tokens.
Finite Automata.
4b Lexical analysis Finite Automata
Finite Automata & Language Theory
Chapter 3. Lexical Analysis (2)
4b Lexical analysis Finite Automata
Presentation transcript:

Pembangunan Kompilator

 A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence of that language, and “no” otherwise.  We call the recognizer of the tokens as a finite automaton.  A finite automaton can be: deterministic(DFA) or non-deterministic (NFA)  This means that we may use a deterministic or non-deterministic automaton as a lexical analyzer.  Both deterministic and non-deterministic finite automaton recognize regular sets.  Which one?  deterministic – faster recognizer, but it may take more space  non-deterministic – slower, but it may take less space  Deterministic automatons are widely used lexical analyzers.  First, we define regular expressions for tokens; Then we convert them into a DFA to get a lexical analyzer for our tokens.  Algorithm1: Regular Expression  NFA  DFA (two steps: first to NFA, then to DFA)  Algorithm2: Regular Expression  DFA (directly convert a regular expression into a DFA)

 A non-deterministic finite automaton (NFA) is a mathematical model that consists of:  S - a set of states   - a set of input symbols (alphabet)  move – a transition function move to map state-symbol pairs to sets of states.  s 0 - a start (initial) state  F – a set of accepting states (final states)   - transitions are allowed in NFAs. In other words, we can move from one state to another one without consuming any symbol.  A NFA accepts a string x, if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x.

102 ab start a b 0 is the start state s 0 {2} is the set of final states F  = {a,b} S = {0,1,2} Transition Function: a b 0 {0,1} {0} 1 _ {2} 2 _ _ Transition graph of the NFA The language recognized by this NFA is (a|b) * a b

A Deterministic Finite Automaton (DFA) is a special form of a NFA. no state has  - transition for each symbol a and state s, there is at most one labeled edge a leaving s. i.e. transition function is from pair of state-symbol to state (not set of states) 102 ba a b The language recognized by this DFA is also (a|b) * a b b a

Le us assume that the end of a string is marked with a special symbol (say eos). The algorithm for recognition will be as follows: (an efficient implementation) s  s 0 { start from the initial state } c  nextchar{ get the next character from the input string } while (c != eos) do{ do until the en dof the string } begin s  move(s,c){ transition function } c  nextchar end if (s in F) then { if s is an accepting state } return “yes” else return “no”

S   -closure({s 0 }){ set all of states can be accessible from s 0 by  -transitions } c  nextchar while (c != eos) { begin s   -closure(move(S,c)){ set of all states can be accessible from a state in S c  nextchar by a transition on c } end if (S  F !=  ) then{ if S contains an accepting state } return “yes” else return “no”  This algorithm is not efficient.

 This is one way to convert a regular expression into a NFA.  There can be other ways (much efficient) for the conversion.  Thomson’s Construction is simple and systematic method. It guarantees that the resulting NFA will have exactly one final state, and one start state.  Construction starts from simplest parts (alphabet symbols). To create a NFA for a complex regular expression, NFAs of its sub-expressions are combined to create its NFA,

To recognize an empty string  To recognize a symbol a in the alphabet  If N(r 1 ) and N(r 2 ) are NFAs for regular expressions r 1 and r 2 For regular expression r 1 | r 2 a fi fi  N(r 2 ) N(r 1 ) fi NFA for r 1 | r 2   

For regular expression r 1 r 2 ifN(r 2 )N(r 1 ) NFA for r 1 r 2 Final state of N(r 2 ) become final state of N(r 1 r 2 ) For regular expression r * N(r)if NFA for r *   

a: a b b: (a | b) a b     b     a    (a|b) *   b     a    a (a|b) * a

put  -closure({s 0 }) as an unmarked state into the set of DFA (DS) while (there is one unmarked S 1 in DS) do begin mark S 1 for each input symbol a do begin S 2   -closure(move(S 1,a)) if (S 2 is not in DS) then add S 2 into DS as an unmarked state transfunc[S 1,a]  S 2 end  a state S in DS is an accepting state of DFA if a state in S is an accepting state of NFA  the start state of DFA is  -closure({s 0 })

S 0 =  -closure({0}) = {0,1,2,4,7} S 0 into DS as an unmarked state  mark S 0  -closure(move(S 0,a)) =  -closure({3,8}) = {1,2,3,4,6,7,8} = S 1 S 1 into DS  -closure(move(S 0,b)) =  -closure({5}) = {1,2,4,5,6,7} = S 2 S 2 into DS transfunc[S 0,a]  S 1 transfunc[S 0,b]  S 2  mark S 1  -closure(move(S 1,a)) =  -closure({3,8}) = {1,2,3,4,6,7,8} = S 1  -closure(move(S 1,b)) =  -closure({5}) = {1,2,4,5,6,7} = S 2 transfunc[S 1,a]  S 1 transfunc[S 1,b]  S 2  mark S 2  -closure(move(S 2,a)) =  -closure({3,8}) = {1,2,3,4,6,7,8} = S 1  -closure(move(S 2,b)) =  -closure({5}) = {1,2,4,5,6,7} = S 2 transfunc[S 2,a]  S 1 transfunc[S 2,b]  S 2 b     a    a

S 0 is the start state of DFA since 0 is a member of S 0 ={0,1,2,4,7} S 1 is an accepting state of DFA since 8 is a member of S 1 = {1,2,3,4,6,7,8} b a a b b a S1S1 S2S2 S0S0

 We may convert a regular expression into a DFA (without creating a NFA first).  First we augment the given regular expression by concatenating it with a special symbol #. r  (r)# augmented regular expression  Then, we create a syntax tree for this augmented regular expression.  In this syntax tree, all alphabet symbols (plus # and the empty string) in the augmented regular expression will be on the leaves, and all inner nodes will be the operators in that augmented regular expression.  Then each alphabet symbol (plus #) will be numbered (position numbers).

(a|b) * a  (a|b) * a #augmented regular expression  *  | b a # a Syntax tree of (a|b) * a # each symbol is numbered (positions) each symbol is at a leave inner nodes are operators

Then we define the function followpos for the positions (positions assigned to leaves). followpos(i) -- is the set of positions which can follow the position i in the strings generated by the augmented regular expression. For example, ( a | b) * a # followpos(1) = {1,2,3} followpos(2) = {1,2,3} followpos(3) = {4} followpos(4) = {}

 To evaluate followpos, we need three more functions to be defined for the nodes (not just for leaves) of the syntax tree.  firstpos(n) -- the set of the positions of the first symbols of strings generated by the sub-expression rooted by n.  lastpos(n) -- the set of the positions of the last symbols of strings generated by the sub-expression rooted by n.  nullable(n) -- true if the empty string is a member of strings generated by the sub-expression rooted by n false otherwise

nnullable(n)firstpos(n)lastpos(n) leaf labeled  true  leaf labeled with position i false{i} | c 1 c 2 nullable(c 1 ) or nullable(c 2 ) firstpos(c 1 )  firstpos(c 2 )lastpos(c 1 )  lastpos(c 2 )  c 1 c 2 nullable(c 1 ) and nullable(c 2 ) if (nullable(c 1 )) firstpos(c 1 )  firstpos(c 2 ) else firstpos(c 1 ) if (nullable(c 2 )) lastpos(c 1 )  lastpos(c 2 ) else lastpos(c 2 ) * c 1 truefirstpos(c 1 )lastpos(c 1 )

 Two-rules define the function followpos: 1. If n is concatenation-node with left child c 1 and right child c 2, and i is a position in lastpos(c 1 ), then all positions in firstpos(c 2 ) are in followpos(i). 2. If n is a star-node, and i is a position in lastpos(n), then all positions in firstpos(n) are in followpos(i).  If firstpos and lastpos have been computed for each node, followpos of each position can be computed by making one depth-first traversal of the syntax tree.

 *  | b a # a {1} {1,2,3} {3} {1,2,3} {1,2} {2} {4} {3} {1,2} {2} green – firstpos blue – lastpos Then we can calculate followpos followpos(1) = {1,2,3} followpos(2) = {1,2,3} followpos(3) = {4} followpos(4) = {} After we calculate follow positions, we are ready to create DFA for the regular expression.

 Create the syntax tree of (r) #  Calculate the functions: followpos, firstpos, lastpos, nullable  Put firstpos(root) into the states of DFA as an unmarked state.  while (there is an unmarked state S in the states of DFA) do  mark S  for each input symbol a do ▪ let s 1,...,s n are positions in S and symbols in those positions are a ▪ S ’  followpos(s 1 ) ...  followpos(s n ) ▪ move(S,a)  S’ ▪ if (S’ is not empty and not in the states of DFA) ▪ put S’ into the states of DFA as an unmarked state.  the start state of DFA is firstpos(root)  the accepting states of DFA are all states containing the position of #

followpos(1)={1,2,3} followpos(2)={1,2,3} followpos(3)={4} followpos(4)={} S 1 =firstpos(root)={1,2,3}  mark S 1 a: followpos(1)  followpos(3)={1,2,3,4}=S 2 move(S 1,a)=S 2 b: followpos(2)={1,2,3}=S 1 move(S 1,b)=S 1  mark S 2 a: followpos(1)  followpos(3)={1,2,3,4}=S 2 move(S 2,a)=S 2 b: followpos(2)={1,2,3}=S 1 move(S 2,b)=S 1 start state: S 1 accepting states: {S 2 } S1S1 S2S2 a b b a

followpos(1)={2} followpos(2)={3,4} followpos(3)={3,4} followpos(4)={} S 1 =firstpos(root)={1,2}  mark S 1 a: followpos(1)={2}=S 2 move(S 1,a)=S 2 b: followpos(2)={3,4}=S 3 move(S 1,b)=S 3  mark S 2 b: followpos(2)={3,4}=S 3 move(S 2,b)=S 3  mark S 3 c: followpos(3)={3,4}=S 3 move(S 3,c)=S 3 start state: S 1 accepting states: {S 3 } S3S3 S2S2 S1S1 c a b b

 partition the set of states into two groups:  G 1 : set of accepting states  G 2 : set of non-accepting states  For each new group G  partition G into subgroups such that states s 1 and s 2 are in the same group iff for all input symbols a, states s 1 and s 2 have transitions to states in the same group.  Start state of the minimized DFA is the group containing the start state of the original DFA.  Accepting states of the minimized DFA are the groups containing the accepting states of the original DFA.

ba a a b b G 1 = {2} G 2 = {1,3} G 2 cannot be partitioned because move(1,a)=2move(1,b)=3 move(3,a)=2move(2,b)=3 So, the minimized DFA (with minimum states) {1,3} a a b b {2}

b b b a a a a b Groups: {1,2,3}{4} a b 1->21->3 2->22->3 3->43->3 {1,2}{3} no more partitioning So, the minimized DFA {1,2} {4} {3} b a a a b b

 The lexical analyzer has to recognize the longest possible string.  Ex: identifier newval -- n ne new newv newva newval  What is the end of a token? Is there any character which marks the end of a token?  It is normally not defined.  If the number of characters in a token is fixed, in that case no problem: + -  But (in Pascal)  The end of an identifier : the characters cannot be in an identifier can mark the end of token.  We may need a lookhead ▪ In Prolog: p :- X is 1. p :- X is 1.5. The dot followed by a white space character can mark the end of a number. But if that is not the case, the dot must be treated as a part of the number.

 Skipping comments  Normally we don’t return a comment as a token.  We skip a comment, and return the next token (which is not a comment) to the parser.  So, the comments are only processed by the lexical analyzer, and the don’t complicate the syntax of the language.  Symbol table interface  symbol table holds information about tokens (at least lexeme of identifiers)  how to implement the symbol table, and what kind of operations. ▪ hash table – open addressing, chaining ▪ putting into the hash table, finding the position of a token from its lexeme.  Positions of the tokens in the file (for the error handling).

Thankyou