1 / 28

280 likes | 432 Views

Lesson 6. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Code generation using syntax-directed translation Lexical analysis. Code generation using syntax-directed translation. Syntax-directed translation. Add attributes to the grammar symbols

Download Presentation
## Lesson 6

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**Lesson 6**CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg**Outline**• Code generation usingsyntax-directedtranslation • Lexicalanalysis**Syntax-directedtranslation**• Add attributes to the grammar symbols • Add semantic actions to the grammar • Syntax-directed translation scheme • “Inject” code into the parser**SDT example(Section2.3 in the book)**• Expression grammar: expr → expr + num | expr – num | num • Infix to postfixnotation**SDT example(Section2.3 in the book)**• Formal definition: • postfix(num) = num • postfix( (E) ) = postfix(E) • postfix(E1opE2) = postfix(E1) postfix(E2) op**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.**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) }**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) }**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) }**Leftrecursion elimination**expr → num { print(num.value) } rest rest → + num { print(num.value);print('+') } rest rest → - num { print(num.value);print('-') } rest rest → ε**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.**Syntax-directed definitions**• Similar to translation schemes • More “abstract” or “declarative”**Lexicalanalysis**• “Lexical analyzer”/“scanner”/“tokenizer” • Simplifies the parser: • Removes white spaces • Removes comments • Identifies lexemes and returns tokens**Tokens**• Name + attribute • Attributes: • Line and columnnumber • Identifiername/symbol table index • Numericalvalue • … • Lexemes**Differingrequirements**• Allowspaces in identifiers? • Example: Fortran 90 • Allowkeywords as identifiers? • Example: PL/1 • Language support for configuringthe lexicalanalysis? • Example: TeX**Implementinglexicalanalysis**• Finite statemachine? • Hard-coded? • Use a generator tool?**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 */ }**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; }**Differentiatingbetweenkeywords and identifiers**• Twostrategies: • Keyword table • Test for keywords before identifiers**Errorrecovery**• Often hard to detect • Misspelled keywords = valid identifiers • Misspelled identifiers hard to detect • Recovery strategies: • Panic mode • Try to “fix” the input**Conclusion**• Code generation usingsyntax-directedtranslation • Lexicalanalysis**Next time**• Stack machinecode • Generating stack machinecodeusing SDT

More Related