Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
? ?? ?
M
{ ? is also often termed as
|
.
{ n computing, |
or ?
, is the process of analyzing a
text, made of a sequence of tokens (for
example, words), to determine its
grammatical structure with respect to a
given (more or less) formal grammar.
|
n computing, a
is one of the components
in an interpreter or compiler, which checks for correct
syntax and builds a data structure (often some kind of
parse tree , abstract syntax tree or other hierarchical
structure) implicit in the input tokens. The parser often
uses a separate Lexical Analysis to create tokens from
the sequence of input characters.
Parsers may be programmed by hand or may be
(semi-)automatically generated (in some programming
languages) by a tool.
?yntax Analysis (Parsing)
input
±?equence of tokens
output
±Abstract ?yntax Tree
?
?ource
program
Parse
tree
Rest of
parser front end
Request
for token
We categorize the parsers into two groups:
|
]erivations
E E+E
E+E derives from E
± we can replace E by E+E
± to able to do this, we have to have a production rule
E0E+E in our grammar.
E E+E id+E id+id
A sequence of replacements of non-terminal symbols is
called a
# of id+id from E.
*
+
n general a derivation step is
%
# .
Left-Most and Right-Most ]erivation
Left-Most ]erivation
lm E -(E)
lm -E lm lm lm -(id+E) -
-(E+E)
(id+id)
rm rm rm rm rm
Right-Most ]erivation
E -E -(E) -(E+E) -(E+id) -
(id+id)
The top-down parsers try to find the left-
most derivation of the given source
program.
E E + E
E
- - E
E
-(id+E) -(id+id)
( E ) ( E )
E + E E + E
id id id
Ambiguity
A grammar produces more than one parse tree for a sentence
is called as an grammar.
id id
E
E E*E E+E*E id+E*E
E * E
id+id*E id+id*id
E + E id
id id
Ambiguity (cont.)
For the most parsers, the grammar must be
unambiguous.
unambiguous grammar
{ unique selection of the parse tree for a
sentence
Ambiguity (cont.)
We should eliminate the ambiguity in the
grammar during the design phase of the
compiler.
An unambiguous grammar should be written to
eliminate the ambiguity.
We have to prefer one of the parse trees of a
sentence (generated by an ambiguous grammar)
to disambiguate that grammar to restrict to this
choice.