# Lesson 6

Télécharger la présentation

## Lesson 6

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Lesson 6 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg

2. Outline • Code generation usingsyntax-directedtranslation • Lexicalanalysis

3. Code generation usingsyntax-directedtranslation

4. Syntax-directedtranslation • Add attributes to the grammar symbols • Add semantic actions to the grammar • Syntax-directed translation scheme • “Inject” code into the parser

5. SDT example(Section2.3 in the book) • Expression grammar: expr → expr + num | expr – num | num • Infix to postfixnotation

6. SDT example(Section2.3 in the book)

7. SDT example(Section2.3 in the book) • Formal definition: • postfix(num) = num • postfix( (E) ) = postfix(E) • postfix(E1opE2) = postfix(E1) postfix(E2) op

8. Exercise (1) Translate the following infix expressions intopostfix notation: • 78 • 3 – 2 – 1 • (8 + 19 * 3) • 3 * (17) / (92 + 8) Assumeconventional operator precedence and associativity.

9. SDT example(Section2.3 in the book) • Translationscheme: expr → expr + num { print(num.value);print('+') } | expr – num { print(num.value);print('–') } | num { print(num.value) }

10. SDT example(Section2.3 in the book) • Extendedparsetree for 1 + 2 – 3: expr expr – num (3) { print(num.value); print('-') } expr + num (2) { print(num.value); print('+') } num (1) { print(num.value) }

11. Exercise (2) Traverse the followingextendedparsetree in a depth-first, left-right order and execute the semanticactions: expr expr – num (3) { print(num.value); print('-') } expr + num (2) { print(num.value); print('+') } num (1) { print(num.value) }

12. Leftrecursion elimination expr → num { print(num.value) } rest rest → + num { print(num.value);print('+') } rest rest → - num { print(num.value);print('-') } rest rest → ε

13. Exercise (3) Draw the parsetree for 1 + 2 – 3(i.e. num + num – num) with the new grammar. Include the semanticactions as leafnodes. Thentraverse it and execute the semanticactions.

14. Syntax-directed definitions • Similar to translation schemes • More “abstract” or “declarative”

15. Lexicalanalysis

16. Lexicalanalysis • “Lexical analyzer”/“scanner”/“tokenizer” • Simplifies the parser: • Removes white spaces • Removes comments • Identifies lexemes and returns tokens

17. Tokens • Name + attribute • Attributes: • Line and columnnumber • Identifiername/symbol table index • Numericalvalue • … • Lexemes

18. Differingrequirements • Allowspaces in identifiers? • Example: Fortran 90 • Allowkeywords as identifiers? • Example: PL/1 • Language support for configuringthe lexicalanalysis? • Example: TeX

19. Implementinglexicalanalysis • Finite statemachine? • Hard-coded? • Use a generator tool?

20. Input buffering

21. intlineno = 1, attribute = NONE; intGetNextToken(void) { char t; for (t = ReadChar(); t != 0; t = ReadChar()) { if (t == ' ' || t == '\t') /* Skipwhitespaces */ elseif (t == '\n') lineno++; elseif ('0' <= t && t <= '9') { attribute= GetNum(t); return NUM; } else { /* Error handling */ attribute= NONE; return UNKNOWN_TOKEN; } } return EOF; /* End of file token */ }

22. intGetNum(char t) { intnum= 0; for(; '0' <= t && t <= '9'; t = ReadChar()) { num*= 10; num+= t – '0'; } // Put back the char that caused the loop to exit PutBack(t); returnnum; }

23. DFA-based scanner

24. DFA-based scanner

25. Differentiatingbetweenkeywords and identifiers • Twostrategies: • Keyword table • Test for keywords before identifiers

26. Errorrecovery • Often hard to detect • Misspelled keywords = valid identifiers • Misspelled identifiers hard to detect • Recovery strategies: • Panic mode • Try to “fix” the input

27. Conclusion • Code generation usingsyntax-directedtranslation • Lexicalanalysis

28. Next time • Stack machinecode • Generating stack machinecodeusing SDT