COMPILERS

9
COMPILERS LECTURE DATE: AUG-14,2013 1 By- Smriti Agrawal 11cs10056

description

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, - PowerPoint PPT Presentation

Transcript of COMPILERS

Page 1: COMPILERS

COMPILERSLECTURE DATE: AUG-14,2013

1By-Smriti Agrawal11cs10056

Page 2: COMPILERS

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

є

>

> =

Page 3: COMPILERS

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}

Page 4: COMPILERS

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

Page 5: COMPILERS

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

Page 6: COMPILERS

<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

Page 7: COMPILERS

Example:

7

Production:

st -> if (expr)then st else st

If : T( : TExpr : V) : TThen : Tst : VElse : T

Page 8: COMPILERS

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

Page 9: COMPILERS

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.