28
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Embed Size (px)

Citation preview

Page 1: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Lesson 6

CDT301 – Compiler Theory, Spring 2011Teacher: Linus Källberg

Page 2: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2

Outline

• Code generation using syntax-directed translation

• Lexical analysis

Page 3: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

CODE GENERATION USING SYNTAX-DIRECTED TRANSLATION

3

Page 4: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Syntax-directed translation

• Add attributes to the grammar symbols• Add semantic actions to the grammar

– Syntax-directed translation scheme• “Inject” code into the parser

4

Page 5: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

SDT example(Section 2.3 in the book)

• Expression grammar:expr → expr + num

| expr – num | num

• Infix to postfix notation

5

Page 6: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

SDT example(Section 2.3 in the book)

6

Infix expression Postfix expression1 + 2 1 2 +(1 + 2) – 3 12 + 3 –1 + (2 – 3) 1 2 3 – +

Page 7: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

SDT example(Section 2.3 in the book)

• Formal definition:– POSTFIX(num) = num– POSTFIX( (E) ) = POSTFIX(E)– POSTFIX(E1 op E2) = POSTFIX(E1) POSTFIX(E2)

op

7

Page 8: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (1)

Translate the following infix expressions into postfix notation:

a) 78b) 3 – 2 – 1c) (8 + 19 * 3)d) 3 * (17) / (92 + 8)Assume conventional operator precedence

and associativity.8

Page 9: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

SDT example(Section 2.3 in the book)

• Translation scheme:expr → expr + num { print(num.value);

print('+') } | expr – num { print(num.value);

print('–') } | num { print(num.value) }

9

Page 10: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

SDT example(Section 2.3 in the book)

• Extended parse tree for 1 + 2 – 3:

10

expr

expr

expr

num (1)

+ num (2)

num (3) { print(num.value); print('-') }

{ print(num.value); print('+') }

{ print(num.value) }

Page 11: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (2)Traverse the following extended parse tree in

a depth-first, left-right order and execute the semantic actions:

11

expr

expr

expr

num (1)

+ num (2)

num (3) { print(num.value); print('-') }

{ print(num.value); print('+') }

{ print(num.value) }

Page 12: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Left recursion elimination

expr → num { print(num.value) } restrest → + num { print(num.value);

print('+') } restrest → - num { print(num.value);

print('-') } restrest → ε

12

Page 13: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Exercise (3)

Draw the parse tree for 1 + 2 – 3(i.e. num + num – num) with the new grammar. Include the semantic actions as leaf nodes. Then traverse it and execute the semantic actions.

13

Page 14: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Syntax-directed definitions

• Similar to translation schemes• More “abstract” or “declarative”

14

Production Semantic rulesexpr → expr1 + num expr.t = expr1.t || num.value || '+' | expr1 – num expr.t = expr1.t || num.value || '-' | num expr.t = num.value

Page 15: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

LEXICAL ANALYSIS

15

Page 16: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Lexical analysis

• “Lexical analyzer”/“scanner”/“tokenizer”

• Simplifies the parser:– Removes white spaces– Removes comments– Identifies lexemes and

returns tokens16

Page 17: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Tokens

• Name + attribute• Attributes:

– Line and column number– Identifier name/symbol

table index– Numerical value– …

• Lexemes 17

Page 18: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Differing requirements

• Allow spaces in identifiers?– Example: Fortran 90

• Allow keywords as identifiers?– Example: PL/1

• Language support for configuringthe lexical analysis?– Example: TeX

18

Page 19: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Implementing lexical analysis

• Finite state machine?• Hard-coded?• Use a generator tool?

19

Page 20: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Input buffering

20

Page 21: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

int lineno = 1, attribute = NONE;int GetNextToken(void) {

char t;for (t = ReadChar(); t != 0; t = ReadChar()) {

if (t == ' ' || t == '\t')/* Skip white spaces */

else if (t == '\n')lineno++;

else if ('0' <= t && t <= '9') {attribute = GetNum(t);return NUM;

}else { /* Error handling */

attribute = NONE;return

UNKNOWN_TOKEN;}

}return EOF; /* End of file token */

}

Page 22: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

22

int GetNum(char t) {int num = 0;for (; '0' <= t && t <= '9'; t = ReadChar()) {

num *= 10; num += t – '0';

}// Put back the char that caused the loop to exitPutBack(t);return num;

}

Page 23: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

DFA-based scanner

23

Page 24: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

DFA-based scanner

24

SymbolState

0 1 2 3 4 5 6 7 8< 1 4 8> 6 3 8= 5 2 7

other 4 8

Page 25: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Differentiating betweenkeywords and identifiers

• Two strategies:– Keyword table– Test for keywords before identifiers

25

Page 26: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Error recovery

• Often hard to detect– Misspelled keywords = valid identifiers– Misspelled identifiers hard to detect

• Recovery strategies:– Panic mode– Try to “fix” the input

26

Page 27: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Conclusion

• Code generation using syntax-directed translation

• Lexical analysis

27

Page 28: Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

Next time

• Stack machine code• Generating stack machine code using SDT

28