Compilers - PowerPoint PPT Presentation

1 / 94
About This Presentation
Title:

Compilers

Description:

... used by the intermediate code generator to create a stream of ... The scanner generally recognizes both single and multiple character tokens directly. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 95
Provided by: Srin2
Category:

less

Transcript and Presenter's Notes

Title: Compilers


1
Compilers
  • Introduction
  • Basic Compiler Function
  • Lexical Analysis,
  • Syntactic Analysis
  • Operator-Precedence Parsing
  • Shift Reduce Parsing
  • Recusive Descent Parsing

N.K. Srinath srinath_nk_at_yahoo.com 1
RVCE
2
Definition Translation of Source language such
as high-level language such as FORTRAN, PL/1,
COBOL, C etc to a low-level language such as
assembly language or machine language is called a
compiler.
Object Program (Assembly level or machine level)
Compiler
Source Program Statements (Programming Language)
3
  • Interpreter
  • Certain translators transform a programming
    language into a simplified language, called
    intermediate code, which can be directly executed
    using a program called an interpreter.
  • Difference between Interpreter and Compiler
  • Interpreters are smaller than compilers.
  • Implementation of complex programming
    language constructs is easier.

4
  • Execution time of an Interpreters program is
    slower than that of a corresponding compiled
    object program.
  • The interpreters need to retain source code,
    object code and the interpreter program in main
    memory that is an overhead.
  • Each time the program is to be executed,
    the interpreter needs to be run.

5
Compilation process is complex. Compilation
process is partitioned into a series of
sub-processes called phases. A phase is a
logically cohesive operation that takes as input
one representation of the source program and
produces as output another representation.
6
Applications
  • In addition to the development of a
  • compiler, the techniques used in
  • compiler design can be applicable to many
    problems in computer science.
  • Techniques used in a lexical analyzer can be
    used in text editors, information retrieval
    system, and pattern recognition programs.
  • Techniques used in a parser can be used in a
    query processing system such as SQL.

7
  • Many software having a complex
  • front-end may need techniques
  • used in compiler design.
  • A symbolic equation solver which takes an
    equation as input. That program should parse
    the given input equation.
  • Most of the techniques used in compiler
    design can be used in Natural Language
    Processing (NLP) systems.

8
Phases of Compilers
Target Program
9
  • Lexical Analysis
  • It is also called as Scanner.
  • It separates characters of the source language
    into groups that logically belong together, these
    groups are called tokens.
  • The usual tokens are
  • Keyword DO, if, FOR, While
  • Identifiers x, num,
  • Operator symbols lt, , , -
  • Punctuation symbols parentheses, commas.

10
  • Each token is considered as an logical entity.
  • There are two kinds of tokens
  • Specific string
  • The specific strings are if, DO, FOR, While
  • classes of strings
  • Classes of strings are identifiers, constants or
    labels.
  • A token may consists of two parts a Token type
    and a token value

11
  • Syntax Analyzer
  • The output of the lexical analysis is a stream of
    tokens, which is passed to the next phase the
    syntax analyzer or parser.
  • Syntax Analyzer groups tokens together into
    syntactic structure. For example, the three
    tokens representing A B might be grouped into
    a syntactic structure called as expression.
  • Expressions might further be combined to form
    statements

12
Syntactic structures can be regarded as a tree
whose leaves are the tokens. The interior nodes
of the tree represent strings of token that
logically belong together. Fig shows the syntax
tree for READ statement in PASCAL. Read( id,
ltid-listgt)
13
  • Intermediate Code generation
  • The structured produced by the syntax analyzer
    is used by the intermediate code generator to
    create a stream of simple instructions.
  • Intermediate code generated does not specify
    the registers to be used for each operation.

14
  • Code Optimization
  • This phase is to reduce the space occupied by
    the generated code.
  • It is also used to reduce the time of
    operation.
  • This phase is optional.

15
  • Code Generation
  • The object code is generated in this phase.
  • It decides the memory location for data.
  • It decides which registers should be used for
    computation.

16
  • Table Management
  • It is used to store the literals,
  • It stores the data types and
  • it stores the data structure used to record this
    information called Symbol table.
  • It Interacts with all the phases of the compiler.

17
  • Error Handler
  • Indicates the error in the source program.
  • It interacts with all the phases.
  • The error information is passed from one phase
    to another.

18
Passes in a Compiler In compiler implementation,
one of more phases are combined into a module
called a pass. The lexical analyzer and the
syntax analyzer are often grouped together into
the same pass.
19
  • GRAMMARS
  • A grammar gives a precise syntactic
    specification for the programs of a particular
    programming language.
  • An efficient parser can be constructed
    automatically from a properly designed grammar.
  • A grammar imparts a structure to a program that
    is useful for its translation into object code
    and for the detection of errors.

20
  • A grammar involves four quantities
  • Terminals
  • Nonterminals
  • a start symbol and
  • productions
  • The basic symbols of which strings in the
    language are composed we shall call terminals.

21
The word token is a synonym for terminal when
we are talking about programming
language. Example Begin, end,
Nontermnals are special symbols that denote
sets of string. The terms syntactic variable
are synonyms for nonterminals. Example
statements, expression, statement-list etc.
22
One nonterminal is selected as the start symbol,
and it denotes the language in which we are truly
interested. Productions define the ways in which
the syntactic categories may be built up from one
another and from the terminals. Each production
consists of a nonterminal, followed by an arrow
() , followed by a string of nonterminals and
terminals.
23
Example For a simple arithmetic expression
mention the terminal symbols and
productions. Terminals Id, , - , /, ( and )
Production Expression expression operator
expression Expression ( expression
) Expression - expression Expression id
24
Operator Operator - Operator
/ Operator The above grammar could be
written by short hand notation as E E A E
(E) -E id A - /
25
  • Parse Trees
  • The choice of the replacement order can be
    represented graphically. This representation is
    called the parse tree.
  • Each interior node of the parse tree is labeled
    by some nonterminal A.
  • The children of the node are labeled from left
    to right by the symbols in the right side of the
    production by which this A was replaced in the
    derivation.

26
Example A XYZ
Draw a parse tree for (id id).
Note Here E is the expression and id is
identifier
27
The list of simplified Pascal grammar is shown in
fig. 1. lt prog gt PROGRAM lt program gt VAR
ltdec - list gt begin lt stmt -
list gt end. 2.    ltprog - namegt id 3.    lt
dec - list gt lt dec gt lt dec - list gt
4.    lt dec gt lt id - list gt lt type
gt 5.    lt type gt integer 6.    lt id - list gt
id lt id - list gt , id 7.    ltstmt -
list gt lt stmt gt ltstmt - list gt lt stmt
gt 8.    lt stmt gt lt assign gt ltread gt
lt write gt lt for gt
28
9.    lt assign gt id lt exp gt
10.  ltexpgt lttermgtlt exp gt lt term gt lt exp gt
- lt term gt 11.  lttermgt factorgtlttermgtltfacto
rgtlttermgt DIV ltfactorgt 12.  lt factorgt id
int (lt exp gt) 13.  lt READgt READ (
lt id - list gt) 14.  lt write gt WRITE ( lt
id - list gt) 15.   lt for gt FOR lt idex -
exp gt Do lt body gt 16.  ltindex - expgt id
lt exp gt To ( exp gt 17.  lt body gt lt
start gt begin lt start - list gt end
Fig. Simplified Pascal
Grammar
29
  • 1 (a) Draw parse trees, according to the grammar
    in fig. pascal grammar for the following
    ltid-listgt S
  • ALPHA
  • (b) ALPHA, BETA, GAMMA

N.K. Srinath srinath_nk_at_yahoo.com 29
RVCE
30
2.Draw Parse tree, according to the
grammar in fig.
for the following
ltexpgtS (a)ALPHABETA (b)ALPHA - BETA
GAMMA. (c) ALPHA DIV (BETA GAMMA)
DELTA 3. Suppose the rules of the grammar for
ltexpgt and lttermgt is as follows ltexpgtlttermgtltex
pgtlttermgtltexpgtDivlttermgt lttermgtltfactorgtlttermgt
ltfactorgtlttermgt-ltfactorgt Draw the parse trees for
the following (i) A1B1 (ii) A1-B1G1
(iii)A1DIV(B1G1)-D1
N.K. Srinath srinath_nk_at_yahoo.com 30
RVCE
31
Lexical Analysis
Lexical Analysis involves scanning the program to
be compiled. Scanned items are recognized
directly as single tokens. These tokens could
be defined as a part of the grammar.
Example ltidgtltlettergtltidgtltlettergtltidgtltdigitgt
ltlettergtABC ... Z ltdigitgt
012 ...9 In such a case the scanner world
recognize as tokens the single characters
A,B,...Z,0,1,...9. The parser could interpret a
sequence of such characters as the language
construct ltidgt.
N.K. Srinath srinath_nk_at_yahoo.com 31
RVCE
32
  • Features
  • The length of the identifiers could be
    restricted.
  • The scanner generally recognizes both single and
    multiple character tokens directly.
  • The scanner output consists of sequence of
    tokens.
  • This token can be considered to have a fixed
    length code.
  • For a Pascal grammar list of integer code for
    each token is provided in table.

N.K. Srinath srinath_nk_at_yahoo.com 32
RVCE
33
N.K. Srinath srinath_nk_at_yahoo.com 33
RVCE
34

Issues in Lexical Analyzer
  • The lexical analyzer has to recognize the
    longest possible string.
  • Ex identifier newva -- n ne new newv
    newva
  • There is no end delimiter for the tokens
    defined.
  • Normally we dont return a comment as a token
    and the comments are only processed by the
    lexical analyzer.
  • Symbol table holds information about
    token.

N.K. Srinath srinath_nk_at_yahoo.com 34
RVCE
35
  • Some scanners enter the identifiers directly
    into a symbol table. The token specifier for the
    identifiers may be a pointer to the symbol table
    entry for that identifier.
  • The entire program is not scanned at one time.
  • Scanner is a operator as a procedure that is
    called by the processor when it needs another
    token.
  • Scanner is responsible for reading the lines of
    the source program and possible for printing the
    source listing.

N.K. Srinath srinath_nk_at_yahoo.com
35 RVCE
36
  • The scanner, except for printing as the
  • output listing, ignores comments.
  • Scanner must look into the language
    characteristics.
  • Example FOTRAN
  • Columns 1 - 5 Statement number
  • Column 6 Continuation of line
  • Column 7-72 Program statement
  • PASCAL Blanks function as delimiters for tokens
  • Statement can be continued freely
  • End of statement is indicated by (semi
    column)

N.K. Srinath srinath_nk_at_yahoo.com
36 RVCE
37
  • Scanners should look into the rules
  • for the formation of tokens.
  • Example 'READ' Should not be considered as
    keyword as it is within quotes. i.e., all string
    within quotes should not be considered as token.
  •   Blanks are significant within the quoted
    string.
  •   Blanks has important factor to play in
    different language
  • Example 1 FORTRAN Statement
  • Do 10 I 1, 100
  • Do is a key word, I is identifier, 10 is the
    statement number.

N.K. Srinath srinath_nk_at_yahoo.com 37
RVCE
38
Statement DO 10 I 1 It is an identifier
Do 10 I 1 Note Blanks are ignored in
FORTRAN statement and hence it is a assignment
statement. In this case the scanner must look
ahead to see if there is a comma (,) before it
can decide in the proper identification of the
character. Example 2 In FORTRAN keywords may
also be used as an identifier. Words such as if,
then, and ELSE might represent either keywords
or variable names.
N.K. Srinath srinath_nk_at_yahoo.com 38
RVCE
39
if (then .EQ. ELSE) then if
then ELSE then if endif
Modeling Scanners as Finite Automata   Finite
automatic provides an easy way to visualize the
operation of a scanner. An algorithm is shown
to recognize a token.
N.K. Srinath srinath_nk_at_yahoo.com
39 RVCE
40
Get first Input-character if
Input-character in 'A' .. ' Z' then
Begin while Input-character in 'A' ..
'Z', ' 0'.. ' 9' do Begin get next
input character if Input_character _
then Begin get next
Input_character Last_Char_is_Underscore
true End if _
Else

N.K. Srinath srinath_nk_at_yahoo.com
40 RVCE
41
Last_Char_Is_Underscorefalse
end while if
Last_Char_Is_Underscore then
return(token-error) else
return (Valid_token) end if first is
'A' .. ' Z' else return
(token-error)
N.K. Srinath srinath_nk_at_yahoo.com 41
RVCE
42
SYNTACTIC ANALYSIS
  • Syntax Analyzer creates the syntactic
    structure of the given source program.
  • Syntax Analyzer is also known as parser.
  • The syntax of a programming is described by a
    context-free grammar (CFG). We will use BNF
    (Backus-Naur Form) notation in the description of
    CFGs.
  • The syntax analyzer (parser) checks whether a
    given source program satisfies the rules implied
    by a context-free grammar or not.

N.K. Srinath srinath_nk_at_yahoo.com 42
RVCE
43
  • If it satisfies, the parser creates
    the parse tree of that program.
  • Otherwise the parser gives the error messages.
  • A context-free grammar
  • gives a precise syntactic specification of a
    programming language.
  • the design of the grammar is an initial phase
    of the design of a compiler.
  • a grammar can be directly converted into a
    parser by some tools.

N.K. Srinath srinath_nk_at_yahoo.com 43
RVCE
44
  • Parser works on a stream of tokens.
  • The smallest item is a token.
  • We categorize the parsers into two groups
  • Top-Down Parser the parse tree is created top
    to bottom, starting from the root.
  • Bottom-Up Parser the parse is created bottom to
    top starting from the leaves

N.K. Srinath srinath_nk_at_yahoo.com 44
RVCE
45
Bottom up Bottom up methods begin with the
terminal nodes of the tree and attempt to
combine these into successively high - level
nodes until the root is reached. Top down Top
down methods begin with the rule of the grammar
that specifies the goal of the analysis ( i.e.,
the root of the tree), and attempt to construct
the tree so that the terminal nodes match the
statement being analyzed.
N.K. Srinath srinath_nk_at_yahoo.com 45
RVCE
46
  • Both top-down and bottom-up
  • parsers scan the input from left
  • to right (one symbol at a time).
  • Efficient top-down and bottom-up parsers can be
    implemented only for sub-classes of context-free
    grammars.
  • LL for top-down parsing
  • LR for bottom-up parsing

N.K. Srinath srinath_nk_at_yahoo.com 46
RVCE
47

Context-Free Grammars
  • Inherently recursive structures of a programming
    language are defined by a context-free grammar.
  • In a context-free grammar, we have
  • A finite set of terminals (in our case, this
    will be the set of tokens)
  • A finite set of non-terminals
    (syntactic-variables)
  • A finite set of productions rules in the
    following form

N.K. Srinath srinath_nk_at_yahoo.com 47
RVCE
48
  • A ? ?
  • where A is a non-terminal and
  • ? is a string of terminals and non-terminals
    (including the empty string)
  • A start symbol (one of the non-terminal symbol)
  • Example
  • E ? E E E E E E E / E
    - E
  • E ? ( E )
  • E ? id

N.K. Srinath srinath_nk_at_yahoo.com 48
RVCE
49

Derivations
E EE EE derives from E we can replace E by
EE to able to do this, we have to have a
production rule EEE in our grammar. E
EE idE idid A sequence of replacements of
non-terminal symbols is called a derivation of
idid from E.
N.K. Srinath srinath_nk_at_yahoo.com 49
RVCE
50

Left-Most Derivation E ? -E ? -(E) ? -(EE) ?
-(idE) ? -(idid) Right-Most Derivation E ? -E
? -(E) ? -(EE) ? -(Eid) ? -(idid) We will see
that the top-down parsers try to find the
left-most derivation of the given source
program. We will see that the bottom-up parsers
try to find the right-most derivation of the
given source program in the reverse order.
N.K. Srinath srinath_nk_at_yahoo.com 50
RVCE
51

Parse Tree
  • Inner nodes of a parse
  • tree are non-terminal symbols.
  • The leaves of a parse tree
  • are terminal symbols.
  • A parse tree can be seen as a graphical
    representation of a derivation.

? -(E)
N.K. Srinath srinath_nk_at_yahoo.com 51
RVCE
52

Ambiguity
A grammar produces more than one parse tree for
a sentence is called as an ambiguous grammar.
E ? EE ? idE ? idEE ? ididE ? ididid
E ? EE ? EEE ? idEE ? ididE ? ididid
N.K. Srinath srinath_nk_at_yahoo.com 52
RVCE
53
  • Operator-Precedence Parsing
  • It is very simple
  • Used in languages where virtually all
    operators are used. Example SNOBOL.
  • Three disjoint relations lt. and .gt are used
    between certain pairs of terminals.
  • If alt. b we say that a yields precedence to
    b.
  • if ab we say that a has same precedence as b.
  • If a .gtb we say that a takes precedence over b.

N.K. Srinath srinath_nk_at_yahoo.com 53
RVCE
54
OPERATOR PRECEDENCE PARSING The bottom up
parsing technique considered is called the
operator precedence method. This method is loaded
on examining pairs of consecutive operators in
the source program and making decisions about
which operation should be performed first.
Example A B C - D (1)
N.K. Srinath srinath_nk_at_yahoo.com 54
RVCE
55
There are two ways of determining what precedence
relation should hold between a pair of
terminals. First Method Intuitive method based
on the traditional notions of associativity and
precedence of operators. The usual procedure of
operation is multiplication and division has
higher precedence over addition and
subtraction. the two operators ( and ), we find
that has lower precedence than . This is
written as ? has lower precedence .
N.K. Srinath srinath_nk_at_yahoo.com 55
RVCE
56
Consider the following grammar for expressions E
E A E (E) -E id (2) A -
/ It is not an operator grammar. If we
substitute for A each of its alternates, we
obtain the following operator grammar E E
E E E E E E / E (E) -E id The
ambiguity with this grammar is that it does not
indicate precedence of relations.
N.K. Srinath srinath_nk_at_yahoo.com 56
RVCE
57
There are two ways of determining what precedence
relation should hold between a pair of
terminals. First Method Intuitive method based
on the traditional notions of associativity and
precedence of operators. This approach will
resolve the ambiguities of grammar shown in (2)
and allow us to resolve the ambiguities. Second
Method An Unambiguous grammar for the
N.K. Srinath srinath_nk_at_yahoo.com 57
RVCE
58
language is constructed first. This grammar
reflects the correct associativity and precedence
in its parse trees. Example For arithmetic
expressions involving , -, , / the grammar is
E E E E E E E E / E (E)
-E id
N.K. Srinath srinath_nk_at_yahoo.com 58
RVCE
59
To construct an unambiguous
grammar, there is a mechanical
method for constructing
operator-precedence relations form it. Example
for the expression
id id id the operator
precedence
relations table
is as follows
precedence
relations table
N.K. Srinath srinath_nk_at_yahoo.com 59
RVCE
60
  • The given expression is considered
    as string.
  • All the nonterminals are removed
    and correct relation ? ,? and ? are
    placed between terminals as per the operator
    precedence relations.
  • is placed at the beginning and the end of
    the string.
  • Example id id id id
  • ? ? ?

N.K. Srinath srinath_nk_at_yahoo.com 60
RVCE
61
Precedence matrix for the grammar Pascal rammar
N.K. Srinath srinath_nk_at_yahoo.com 61
RVCE
62
For a Pascal Grammar the precedence for some of
the tokens are explained. Example PROGRAM?VAR
These two tokens have equal precedence Begin ?
FOR begin has lower precedence over FOR.
There are some values which do not follow
precedence relations for comparisons. Example
? end and end ? i.e., when is
followed by end, the ' ' has higher precedence
and when end is followed by the end has higher
precedence.
N.K. Srinath srinath_nk_at_yahoo.com 62
RVCE
63
In all the statements where precedence
relation does not exist in the table,
two tokens cannot appear
together in any legal statement. If such
combination occurs during parsing it should be
recognized as error. Example Pascal
Statement begin READ (VALUE)
These Pascal statements scanned from left to
right, one token at a time. For each pair of
operators, the precedence relation between them
is determined.
N.K. Srinath srinath_nk_at_yahoo.com 63
RVCE
64
  • . . . begin READ ( id )

    ? ? ? ?
  • 2. . . . begin READ ( lt N1 gt )
    (N1)
  • ? ? ?
    ?
  • id

  • Value

N.K. Srinath srinath_nk_at_yahoo.com 64
RVCE
65
. . . begin lt N2 gt
ltN2 gt
READ ( ltN1gt )
N.K. Srinath srinath_nk_at_yahoo.com 65
RVCE
66
  • Example Show a step-by-step parsing for the
    assignment
  • VARIANCE SUMSQ DIV 100 - MEAN MEAN
  • . . id 1 id 2 DIV int -
    id3 id4
  • ? ? ?
  • Left to right scan is continued in each step
    only far enough to determine the next portion of
    the statement to be recognized, which is the
    first portion delimited by ? and ?.
  • Once this portion has been determined, it is
    interpreted as a nonterminal according t some
    rule of the grammar.

N.K. Srinath srinath_nk_at_yahoo.com 66
RVCE
67
Parse tree is constructed from the terminal nodes
up towards the root, hence the term bottom-up
parsing.
The id SUMSQ is interpreted as the single
nonterminal ltN1gt, which is an operand of the
DIV. That is, ltN1gt in the tree corresponds to two
non terminals, ltfactorgt and lttermgt as per the
pascal grammar.
ii . . . id 1 ltN1gt DIV int -
id3 id4
? ? ? ?
N.K. Srinath srinath_nk_at_yahoo.com 67
RVCE
68
iii . id 1 ltN1gt DIV ltN2gt- id3 id4
? ?
? iv id 1 ltN3gt - id3 id4

? ? ? ?
N.K. Srinath srinath_nk_at_yahoo.com 68
RVCE
69
v id 1 ltN3gt - ltN4gt id4

? ? ? ?
? vi id 1 ltN3gt - ltN4gt ltN5gt

? ? ?
? vii id 1 ltN3gt - ltN6gt

? ? ?
N.K. Srinath srinath_nk_at_yahoo.com 69
RVCE
70
.. id1 ltN7gt
?
? ?
N.K. Srinath srinath_nk_at_yahoo.com 70
RVCE
71
ltN8gt
N.K. Srinath srinath_nk_at_yahoo.com 71
RVCE
72
  • SHIFT REDUCE PARSING
  • The operator precedence parsing is bottom up
    parsing.
  • It was developed to shift reduce parsing.
  • This method makes use of a stack to store tokens
    that have not yet been recognized in terms of the
    grammar.
  • The actions of the parser are controlled by
    entries in a table, which is somewhat similar to
    the precedence matrix.

N.K. Srinath srinath_nk_at_yahoo.com 61
RVCE
73
  • The two main actions of shift reducing parsing
    are
  • Shift Push the current token into the stack.
  • Reduce Recognize symbols on top of the stack
    according to a rule of a grammar
  • Example begin READ ( id ) . . .
  • Steps Token Stream
  • 1. . . . begin READ ( id ) . . .

Stack
Begin is first pushes the token on stack
Shift
N.K. Srinath srinath_nk_at_yahoo.com 62
RVCE
74
2. . . . begin READ ( id ) . . . 3. . . .
begin READ ( id ) . . .
The next token READ is also shifted on to the
stack.
N.K. Srinath srinath_nk_at_yahoo.com 63
RVCE
75
3. . . . begin READ ( id ) . . .
4. . . . begin READ ( id ) . . .
5. . . . begin READ ( id ) . . .
When it parser examines the token ), the reduce
action is invoked.
N.K. Srinath srinath_nk_at_yahoo.com 64
RVCE
76
A set of tokens from the top of the
stack is reduced to anon
terminal
symbol from the grammar. It is pushed onto the
stack, to be reduced later as part of the READ
statement. Note Shift action is taken by an
operator-precedence parser when it encounters the
relations ? and ? . Reduce action is taken
when an operator
precedence parser encounters the relation ?.
N.K. Srinath srinath_nk_at_yahoo.com 65
RVCE
77
RECURSIVE DESCENT PARSING
  • Recursive-Descent is a top-down parsing
    technique.
  • It is made up of a procedure for each
    non-terminal symbol in the grammar.
  • A procedure is associated to find a sub-string
    of the input.
  • During this process it may call other
    procedures, or call itself recursively to search
    for other non-terminals.
  • If the procedure finds the non-terminal it
    returns success else failure.

N.K. Srinath srinath_nk_at_yahoo.com 66
RVCE
78
Example ltreadgt READ (ltid-listgt) The
procedure for READ statement is provided which is
derived from the grammar. The grammar considered
are ltid - listgt id , id unlike the one
considered earlier.   lt id - list gt id
ltid-listgt, id
This is because of the fundamental difficulty. If
the procedure decided to try the second
alternative
N.K. Srinath srinath_nk_at_yahoo.com 67
RVCE
79
(ltid-listgt, id), it would immediately
call itself recursively to find an ltid-listgt.
This would result in another
immediate recursive call, which leads to unending
chain. Note that an integer code is provided for
each token as listed earlier. PROCEDURE READ
begin FOUND FALSE if TOKEN 8 READ
then
N.K. Srinath srinath_nk_at_yahoo.com 68
RVCE
80
The READ procedure has been invoked and has
examined the tokens READ and the stream indicated
by dashed lines.
begin advance to next token if TOKEN 20
( then begin advance to next token
if IDLIST returns
success then
N.K. Srinath srinath_nk_at_yahoo.com 69
RVCE
81
READ has called IDLIST (indicated by solid line),
which has examined the token id.
if token 21 ) then begin
FOUND TRUE advance to
next token end if ) end
if ( end if READ
N.K. Srinath srinath_nk_at_yahoo.com 70
RVCE
82
if FOUND TRUE then return success
else return failure end (READ)
IDLIST has returned to READ, indicating
success. Note The parse tree was constructed
beginning at the root, hence the term top-down
parsing.
N.K. Srinath srinath_nk_at_yahoo.com 71
RVCE
83
Procedure for the IDLIST This
procedure checks whether the id-list is as per
the grammar. Procedure IDLIST begin FOUND
FALSE if TOKEN 22 id then
begin FOUND TRUE
advance to Next token
N.K. Srinath srinath_nk_at_yahoo.com 72
RVCE
84
A flag is set to indicate that the identifier
is found and is ok. It
checks for the ,
in the next statement while (TOKEN 14 ,) and
(FOUND TRUE) do begin advance to next
token if TOKEN 22 id then
advance to next token else
FOUND FALSE end
while end if id
N.K. Srinath srinath_nk_at_yahoo.com 73
RVCE
85
if FOUND TRUE then return success
else return failure end IDLIST
If , is not followed by an id the function
returns failure. eg. id,id, - Is an error
Recursive descent parse of the Assignment
Statement procedure and the parse tree is shown
for the nonterminal symbols. ltassigngt id
ltexpgt
N.K. Srinath srinath_nk_at_yahoo.com 74
RVCE
86
Procedure ASSIGN begin FOUND
FALSE if TOKEN 22 id then
begin advance to Next token
if TOKEN 15 then
begin advance to next token
if EXP returns success then
FOUND TRUE end
if end end id
N.K. Srinath srinath_nk_at_yahoo.com 75
RVCE
87
if FOUND TRUE then
return success else return
failure end ASSIGN
Parse tree for Assignment
N.K. Srinath srinath_nk_at_yahoo.com 76
RVCE
88
Grammar for expression is ltexpgt lttermgt
lttermgt - lttermgt Procedure EXP begin
FOUND FALSE If TERM returns success then
begin FOUND TRUE while ((TOKEN
16)or (TOKEN 17 -))
and (FOUND TRUE) do
N.K. Srinath srinath_nk_at_yahoo.com 77
RVCE
89
begin advance to next
token if TERM returns success
then FOUND FALSE end
while end if TERM if FOUND
TRUE then return success else return
failure end EXP
N.K. Srinath srinath_nk_at_yahoo.com 78
RVCE
90
Parse Tree for ASSIGN - TERM
N.K. Srinath srinath_nk_at_yahoo.com 79
RVCE
91
Procedure TERM begin FOUND FALSE
If FACTOR returns success then
begin FOUND TRUE while ((TOKEN
18 ) or (TOKEN 19
DIV ) and (FOUND TRUE) do
begin advance to next token
if TERM returns
failure then
FOUND FALSE
N.K. Srinath srinath_nk_at_yahoo.com 80
RVCE
92
end while end if FACTOR
if FOUND TRUE then return
success else return failure
end TERM The TERM calls the procedure factor.
If it returns success then it proceeds. The
Factor procedure is as follows
N.K. Srinath srinath_nk_at_yahoo.com 81
RVCE
93
Procedure FACTOR begin FOUND FALSE
if (TOKEN22id) or (TOKEN23int) then
begin FOUND TRUE
advance to next token end if id or int
else if TOKEN 20 (
then begin advance to
next token
N.K. Srinath srinath_nk_at_yahoo.com 82
RVCE
94
if EXP returns success then if TOKEN 21 )
then begin (FOUND TRUE)
advance to next token end if )
end if ( if FOUND TRUE
then return success else return
failure end FACTOR
N.K. Srinath srinath_nk_at_yahoo.com 83
RVCE
Write a Comment
User Comments (0)
About PowerShow.com