Post on 02-Jan-2016
description
COMPILERSLECTURE DATE: AUG-14,2013
1By-Smriti Agrawal11cs10056
Algorithm to convert NFA to DFA
2
NFA: has x no. of statesÞ DFA: has atmost 2^x no. of states
• Algorithm should be such that we have to simulate NFA to DFA instead of converting to DFA
• Let S0 be start state of NFA, then start state of DFA (S): є- closure(S0)
• Always the longest string is preferred. So if there is >=< then (>=) will be recognised and one token and (<) will be recognised as another token.
1
2
4 5 6
3є
є
>
> =
Algorithm:
3
C= read_character()While (c!=‘\0’){
S(set of states of NFA) = є-closure ( δ(s,c))c= read_nextchar()
}
If(S∩F != ɸ){
acceptance state}Else{
Reject}
Syntax Analyzer
4
There are syntactic rules for a particular Grammar.
A grammar:• Precisely specify syntax rules of programming
language• Automatically generate those parser which
will detect the correctness of programming language
• Report errors• Add new rules to make the language more
powerful by improving the functionality of grammar
5
Programming definition
CFGFG
ParserProgramme Parse Tree
Directed by specification/grammatical rules
Syntax Rules
• Parser will report some of the possible ambiguities of programming language definition(CFG)
• Output file should contain tokens.• Generate errors if it fails to construct parse tree
<V, T, S, P>
6
• V: Variables,• T: Terminal set which contains set of symbols
which construct the string-Terminals are mapped with tokens
• S: S -> S є V where S is the Start variable• P: Production: rules which combine terminals
and non terminals
Example:
7
Production:
st -> if (expr)then st else st
If : T( : TExpr : V) : TThen : Tst : VElse : T
8
• S w, a string of terminals . If by some production /derivation we can create w from s, then we say it is accepted.
L(G) CFG (Set of strings accepted by that grammar)
Derivations:
E E + EE (E)E -EE Id
a A b a g b A g
9
S a A b Sentential form which is a combination of terminals and non terminals( NT , T)If it contains only terminals, then it is called sentence
• After deriving, how to move from 1. During derivation it should be known which non terminal to replace.2. Secondly, it should be known which rule to be used.
A gHere, A is the head and g is the body of the production.
For example:
E (E) -1 (E+E) -2 ( id + E) -3 (id + id) -4
In the above derivation, thus to avoid conflict we should know whether to replace left or right E by id in the 3rd step of derivation.