90 likes | 207 Views
COMPILERS. LECTURE DATE: AUG-14,2013. By- Smriti Agrawal 11cs10056. Algorithm to convert NFA to DFA. 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,
E N D
COMPILERS LECTURE DATE: AUG-14,2013 By- SmritiAgrawal 11cs10056
Algorithm to convert NFA to DFA • 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. > 3 2 є 1 > = 5 4 6 є
Algorithm: 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 • 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
Programming definition CFGFG Directed by specification/grammatical rules Syntax Rules Programme Parser Parse Tree • 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> • 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: Production: st-> if (expr)then st else st If : T ( : T Expr : V ) : T Then : T st : V Else : T
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 + E • E (E) • E -E • E Id • a Ab a g b • A g
S a Ab 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 g • Here, 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.