1 / 22

Chapter 2-2 A Simple One-Pass Compiler

Chapter 2-2 A Simple One-Pass Compiler. Syntax-directed translation. Syntax-directed definition  translation of a construct in terms of attributes associated with its syntactic components Syntactic structure :: context-free grammar

Download Presentation

Chapter 2-2 A Simple One-Pass Compiler

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

Presentation Transcript


  1. Chapter 2-2A Simple One-Pass Compiler

  2. Syntax-directed translation • Syntax-directed definition  translation of a construct in terms of attributes associated with its syntactic components • Syntactic structure :: context-free grammar • Grammar symbol :: a set of attributes and with each production, a set of semantic rules for computing values of the attributes associated with the symbols appearing in the production • Translation  input-output mapping • Annotated parse tree  a parse tree showing the attribute values at each node

  3. Synthesized Attributes • An attribute is said to be synthesized if its value at a parse-tree node is determinedfrom attribute values at the children of node • 예제 설명

  4. Syntax-directed definition for infix to postfix translation.

  5. Attribute values at nodes in a parse tree.

  6. Depth first traversals • Robot positioning :: • seq  seq instr | begin instr  east | north | west | south • Depth-first traversals • Translation Schemes • Context-free grammar in which program fragments called semantic actions are embedded • Emitting a Translation

  7. Annotated parse tree for begin west south.

  8. Syntax-directed definition of the robot’s position.

  9. Actions translating expressions into postfix notation.

  10. Actions translating 9-5+2 into 95-2+

  11. Top-down parsing • Lookahead … the current token being scanned • Array[ num dotdot num ] of integer • 과정 설명

  12. type simple |  id | array[ simple ]of type Simple  integer | char | num dotdot num (2.8)

  13. (a) type type Array [ simple ] of type type (c) Array [ simple ] of type num dotdot num type (d) Array [ simple ] of type num dotdot num simple type (e) Array [ simple ] of type num dotdot num simple integer Steps in the top-down construction of a parse tree

  14. Predictive parsing • First • 예제로 설명 • ∈-production • Designing a Predictive Parser • Left Recursion • expr  expr + term • 일반화

  15. expr  term rest rest  + term { print(‘+’) } rest | - term { print(‘-’) } rest | term  0 { print (‘0’) } term  1 { print (‘1’) } . . . term  9 { print (‘9’) } (2.14)

  16. Translation of 9 – 5 + 2 into 95 – 2 +. expr term rest 9 {print(‘9’)} - term {print(‘-’)} rest 5 {print(‘5’)} + term {print(‘+’)} rest 2 {print(‘2’)}

  17. expr() { term(); rest(); } rest() { if(lookahead == ‘+’) { match(‘+’); term(); putchar(‘+’); rest(); } else if (lookahead == ‘-’) { match(‘-’); term(); putchar(‘-’); rest(); } else; } term() { if (isdigit(lookahead)) { putchar(lookahead); match(lookahead); } else error; } Fig. 2. 22. Functions for the nonterminals expr, rest, and term.

  18. Replacement for functions expr and rest of Fig. 2.22. expr() { term(); while(1) if(lookahead == ‘+’) { match(‘+’); term(); putchar(‘+’); } else if (lookahead == ‘-’) { match(‘-’); term(); putchar(‘-’); } else break; }

  19. Implementing the interactions in Fig. 2. 25. lexan() lexical analyzer uses getchar() return token to read character to caller pushes back c using ungetc(c, stdin) sets global variable to attribute value tokenval

  20. Symbol table and array for storing strings.

  21. stmt ifexprthenstmt1 { out := newlable; stmt.t := expr.t || ‘gofalse’ out || stmt1.t || ‘label’ out (2.18) WHILE IF Code layout for conditional and while statements.

  22. Fig. 4.15. Parsing table M for grammar (4.11)

More Related