1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank....

13
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as soon as it is encountered LALR(1) parsers may perform a few reductions after the error has been encountered, and then detect it. This is a result to the state merging.

Transcript of 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank....

Page 1: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

1

Error detection in LR parsing

Errors are discovered when a slot in the action table is blank.

Canonical LR(1) parsers detect and report the error as soon as it is encountered

LALR(1) parsers may perform a few reductions after the error has been encountered, and then detect it. This is a result to the state merging.

Page 2: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

2

Error recovery in LR parsing

Phase-level recovery Associate error routines with the empty table slots.

Figure out what situation may have cause the error and make an appropriate recovery.

Panic-mode recovery Discard symbols from the stack until a non-terminal

is found. Discard input symbols until a possible lookahead for that non-terminal is found. Try to continue parsing.

Error productions Common errors can be added as rules to the

grammar.

Page 3: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

3

Error recovery in LR parsing

Phase-level recovery Consider the table for grammar EE+E | id

+ id $ E0 e1 s2 e1 11 s3 e2 accept2 r(Eid) e3 r(Eid)3 e1 s2 e1 44 r(EE+E) e2 r(EE+E)

Error e1: "missing operand inserted". Recover by inserting an imaginary identifier in the stack and shifting to state 2.

Error e2: "missing operator inserted". Recover by inserting an imaginary operator in the stack and shifting to state 3

Error e3: ??

Page 4: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

4

Error recovery in LR parsing

Error productions Allow the parser to "recognize" erroneous input. Example:

statement : expression SEMI | error SEMI | error NEWLINE

If the expression cannot be matched, discard everything up to the next semicolon or the next new line

Page 5: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

5

LR(1) grammars

Does right-recursion cause a problem in bottom-up parsing? No, because a bottom-up parser defers reductions

until it has read the whole handle.

Are these grammars LR(1)? How about LL(1)?

SAa | BbAcBc

LR(1): YESLL(1): NOLL(2): YES

SAa | BbAcA | aBcB | b

LR(1) : YESLL(k) : NO

SAca | BcbAcBc

LR(1): NOLL(1): NOLL(2): NOLR(2): YES

Page 6: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

6

Where are we? Ultimate goal: generate machine code. Before we generate code, we must collect information

about the program: Front end

scanning (recognizing words) CHECK parsing (recognizing syntax) CHECK semantic analysis (recognizing meaning)

There are issues deeper than structure. Consider:

int func (int x, int y);int main () {

int list[5], i, j;char *str;j = 10 + 'b';str = 8;m = func("aa", j, list[12]);return 0;

}

This code is syntacticallycorrect, but will not work.What problems are there?

Page 7: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

7

Beyond syntax analysis

An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what type of arguments

does it take? Is x declared before being used? Where can x be stored? Is the expression x+y type-consistent?

Semantic analysis is the phase where we collect information about the types of expressions and check for type related errors.

The more information we can collect at compile time, the less overhead we have at run time.

Page 8: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

8

Semantic analysis

Collecting type information may involve "computations" What is the type of x+y given the types of x and y?

Tool: attribute grammars CFG Each grammar symbol has associated attributes The grammar is augmented by rules (semantic actions)

that specify how the values of attributes are computed from other attributes.

The process of using semantic actions to evaluate attributes is called syntax-directed translation.

Examples: Grammar of declarations. Grammar of signed binary numbers.

Page 9: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

9

Attribute grammars

Production Semantic ruleD T L L.in = T.typeT int T.type = integerT char T.type = characterL L1, id L1.in = L.in

addtype (id.index, L.in)L id addtype (id.index, L.in)

Example 1: Grammar of declarations

Page 10: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

10

Attribute grammars

Production Semantic ruleN S L if (S.neg)

print('-'); else print('+'); print(L.val);

S + S.neg = 0 S – S.neg = 1L L1, B L.val = 2*L1.val+B.valL B L.val = B.valB 0 B.val = 0*20

B 1 B.val = 1*20

Example 2: Grammar of signed binary numbers

Page 11: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

11

Attributes

Attributed parse tree = parse tree annotated with attribute rules

Each rule implicitly defines a set of dependences Each attribute's value depends on the values of other

attributes. These dependences form an attribute-dependence graph. Note:

Some dependences flow upward The attributes of a node depend on those of its children We call those synthesized attributes.

Some dependences flow downward The attributes of a node depend on those of its parent or

siblings. We call those inherited attributes.

How do we handle non-local information? Use copy rules to "transfer" information to other parts of the

tree.

Page 12: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

12

Attribute grammarsProduction Semantic ruleE E1+E2 E.val = E1.val+E2.valE num E.val = num.yylvalE (E1) E.val = E1.val

E

E E

( E )

+

E +

num

num

E

num

12

10

7

2

3

attribute-dependence graph

Page 13: 1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.

13

Attribute grammars We can use an attribute grammar to construct an AST The attribute for each non-terminal is a node of the tree. Example:

Notes: yylval is assumed to be a node (leaf) created during scanning. The production E (E1) does not create a new node as it is not

needed.

Production Semantic ruleE E1+E2 E.node = new PlusNode(E1.node,E2.node)E num E.node = num.yylval E (E1) E.node = E1.node