





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















































interm repres
errors
lexical analyzer parser^
rest of front end
symbol table
source program
parse get next tree token
token
regular expressions
Syntax Error Identification / Handling
Recall typical error types:
Lexical : Misspellings Syntactic : Omission, wrong order of tokens Semantic : Incompatible types Logical : Infinite loop / recursive call
Majority of error processing occurs during syntax analysis
NOTE: Not all errors are identifiable !! Which ones?
#include<stdio.h> int f1(int v) { int i,j=0; for (i=1;i<5;i++) { j=v+f2(i) } return j; } int f2(int u) { int j; j=u+f1(uu) return j; } int main() { int i,j=0; for (i=1;i<10;i++) { j=j+iI; printf(“%d\n”,i); printf("%d\n",f1(j)); return 0; }
Which are “easy” to recover from? Which are “hard”?
As reported by MS VC++ 'f2' undefined; assuming extern returning int syntax error : missing ';' before ‘}‘ syntax error : missing ';' before ‘return‘ fatal error : unexpected end of file found
Panic Mode – Discard tokens until a “synchro” token is
found ( end, “;”, “}”, etc. ) -- Decision of designer -- Problems: skip input ⇒miss declaration – causing more errors ⇒miss errors in skipped material -- Advantages: simple ⇒suited to 1 error per statement Phase Level – Local correction on input -- “,” ⇒”;” – Delete “,” – insert “;” -- Also decision of designer -- Not suited to all situations -- Used in conjunction with panic mode to allow less input to be skipped
Reg. Lang. CFLs
EXAMPLE: an^ bn^ , n ≥ 1 : Is it regular?
Regular Languages
Token Structure
Scanner Generation
Context Free Languages
Sentence Structure
Parser Generation
Context Free Languages
Reg. Languag es
assign_stmt → id := expr ; expr → term operator term term → id term → real term → integer operator → + operator → -
What do “BLUE” symbols represent?
Derivation: A sequence of grammar rule applications and substitutions that transform a starting non-term into a collection of terminals / tokens.
Simply stated: Grammars / production rules allow us to “rewrite” and “identify” correct syntax.
What do “BLACK” symbols represent?
Given the rules on the previous slide, suppose
id := real + int;
is input. Is it syntactically correct? How do we know?
expr is represented as: expr → term operator term
Is this accurate / complete?
expr → expr operator term
expr → term
How does this affect the derivations which are possible?
Let’s derive: id := id + real – integer ; assign_stmt assign_stmt → id := expr ; → id := expr ; expr → expr operator term → id := expr operator term; expr → expr operator term → id := expr operator term operator term; expr → term → id := term operator term operator term; term → id → id := id operator term operator term; operator → + → id := id + term operator term; term → real → id := id + real operator term; operator → - → id := id + real - term; term → integer → id := id + real - integer; assign_stmt → id := expr ; expr → expr operator term expr → term term → id term → real term → integer operator → + operator → - Docsity.com
expr → expr op expr
expr → ( expr )
expr → - expr
expr → id
op → +
op → -
op → __*
op → /
op → ↑
Black : NT Blue : T expr : S 9 Production rules
To simplify / standardize notation, we offer a synopsis of terminology.
A step in a derivation is zero or one action that replaces a NT with the RHS of a production rule. EXAMPLE: E ⇒ -E (the ⇒ means “derives” in one step) using the production rule: E → -E EXAMPLE: E ⇒ E A E ⇒ E * E ⇒ E * ( E ) DEFINITION: ⇒ derives in one step ⇒ derives in ≥ one step ⇒ derives in ≥ zero steps
EXAMPLES: α A β ⇒ α γ β if A→ γ is a production rule
α 1 ⇒ α 2 ⇒… ⇒ αn → α 1 ⇒ αn ; α ⇒ α for all α
If α ⇒ β and β → γ then α ⇒ γ
Let G be a CFG with start symbol S. Then S ⇒W (where W has no non-terminals) represents the language generated by G, denoted L(G). So W∈L(G) ⇔ S ⇒W.
W : is a sentence of G
When S ⇒α (and α may have NTs) it is called a sentential form of G.
EXAMPLE: id * id is a sentence Here’s the derivation: E ⇒ E A E ⇒E * E ⇒ id * E ⇒ id * id
E ⇒ id * id
Sentential forms