Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.

Similar presentations


Presentation on theme: "1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your."— Presentation transcript:

1 1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your language) Homework #4 due Thursday (Grammar modifications) Coming up: Lab2a & Lab2b posted. Test#1 next week Grammar Modifications Recursive Descent Parser

2 2 Lab1 Data structure for Symbol Table List (dynamic) Dynamic Array std::vector Dynamically allocate more when needed but it’s done in binary (2, 4, 8, 16, etc.) symbol * mySymbolArray Dynamically allocate more space when needed (how many more at a time?) Map Maps string (name) to more info about the name Sorted Binary search tree (red/black tree). Tree is always balanced. Unordered map STL’s hashtable

3 3 Preprocessor / Symbol Table Given the following code snippet: #define MAX 5 void main() { int x = 5; int y = 6; …… #if x == 5 //do something #endif const int MIN = 0; Add (MAX 5) to the symbol table main, x,y are not added to the symbol table in the preprocessor There is no preprocessor symbol called x in the symbol table Why? This can be added to the symbol in preprocessor because it’s just a constant that’s not going to change

4 4 Lab1 check-off Schedule Wednesday: I will be in and out most of the day but can check-off labs whenever I am on. Thursday: I will be available in the morning for lab check-off and again in late afternoon.

5 5 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table skeletal source program preprocessor

6 6 Grammar Example S -> E E -> E + E E -> E * E E -> id This grammar is ambiguous.

7 7 Revised & Expanded Grammar Example S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id S (i) E ET T F = ; + * F T id (a) F i = a + b * c; id (b) id (c)

8 8 But… S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id This grammar doesn’t work for top-down because of left recursion

9 9 In-Class Exercise #6 S -> id = E ; E -> E + T | E – T | T T ->T * F | T / F | F F -> ( E ) | id Remove left-recursion from this grammar

10 10 Recursive Descent Parser (RDP) Is a top down parser Start with grammar modifications MUST remove all left recursion from the grammar. Try to remove all unit productions. Try to left factor so the grammar is one-token look ahead. Input to the parser is a list of tokens. Output: Yes/No: Did the input parse? Parse structure: representing all the statements.

11 11 Grammar HW#2 Let’s look at the grammar for HW#2 Identify left recursion Identify opportunities for one-token look ahead Identify unit productions

12 12 HW#2 Grammar COMPOUND_STAT -> begin OPTIONAL_STAT end STATEMENT -> VARIABLE := EXPRESSION | COMPOUND_STAT | PROCEDURE_CALL | if EXPRESSION then STATEMENT else STATEMENT | while EXPRESSION do STATEMENT VARIABLE -> id PROCEDURE_CALL -> id | id ( EXPR_LIST )

13 13 Recursive Descent Parser RDP Lab1 List of Tokens Each token is a pair (Value, Type) void keyword main ID ( symbol ) symbol { symbol int keyword

14 14 Recursive Descent Parser RDP Lab1 List of Tokens Each token is a pair (Value, Type) void keyword main ID ( symbol ) symbol Tokens can be in a separate file

15 15 Recursive Descent Parser There are 2 types of rules in the grammar: With productions Without productions Example: Field -> `[´ Exp `]´ `=´ Exp | Name `=´ Exp | Exp Funcname2 -> ‘.’ Name Funcname2 |

16 16 Recursive Descent Parser Load tokens into a buffer. Need ability to pull out tokens but also put them back if you can’t use them in a rule. (Backtracking) void main ID ( ) current

17 17 Recursive Descent Parser For every rule in the grammar, generate a bool function. This answers the yes/no question first. Non- rules: If tokens match the rule, return true. If tokens do not match the rule, return false. rules: This means that this rule doesn’t match ANY tokens. It’s not wrong, it’s just that it doesn’t match anything at this point in the matching. One way to handle it is to just return True and let other rules handle the next token.

18 18 Bool Function S -> a S b | c bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else if currentToken == c return True; else return False; }

19 19 Bool Function S -> a S b | bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else return True; //for }

20 20 Bool Function S -> a S b | c bool S() { if currentToken == a if (S()) if nextToken == b return True; else return False; else return False; else if currentToken == c return True; else return False; } Parser is a pushdown automata But where is the “stack”? Call stack

21 21 Bool function Field -> `[´ Exp `]´ `=´ Exp | Name `=´ Exp | Exp bool Field() { if currentToken == ‘[‘ if (Exp()) if nextToken == ‘]’ if nextToken == ‘=‘ if (Exp()) return True; else if currentToken == Name if nextToken == ‘=‘ if (Exp()) return True; else if (Exp()) return True; } Return false for all other conditions May need to put tokens back before checking this rule

22 22 Bool function Funcname2 -> ‘.’ Name Funcname2 | bool Funcname2() { if currentToken == ‘.‘ if (nextToken == Name) if (Funcname2()) return True; else return True; //for.. Don’t remove //any tokens }


Download ppt "1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your."

Similar presentations


Ads by Google