Ch4a - 1 ! Syntax Analysis" Part I! Chapter 4! COP5621...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Position of a Parser in the Compiler Model Lexical Analyzer Parser and rest of front-end Source Program Token, tokenval Symbol Table Get next token Lexical error Syntax error Semantic error Intermediate representation
Background image of page 2
3 The Parser A parser implements a C-F grammar The role of the parser is twofold: 1. To check syntax (= string recognizer) And to report syntax errors accurately 2. To invoke semantic actions For static semantics checking, e.g. type checking of expressions, functions, etc. For syntax-directed translation of the source code to an intermediate representation
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Syntax-Directed Translation • One of the major roles of the parser is to produce an intermediate representation (IR) of the source program using syntax-directed translation methods • Possible IR output: – Abstract syntax trees (ASTs) – Control-Fow graphs (C±Gs) with triples, three-address code, or register transfer list notation – WHIRL (SGI Pro64 compiler) has 5 IR levels!
Background image of page 4
5 Error Handling • A good compiler should assist in identifying and locating errors Lexical errors : important, compiler can easily recover and continue Syntax errors : most important for compiler, can almost always recover Static semantic errors : important, can sometimes recover Dynamic semantic errors : hard or impossible to detect at compile time, runtime checks are required Logical errors : hard or impossible to detect
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Viable-Prefx Property • The viable-prefx property oF parsers allows early detection oF syntax errors – Goal: detection oF an error as soon as possible without Further consuming unnecessary input – How: detect an error as soon as the prefx oF the input does not match a prefx oF any string in the language for (;) DO 10 I = 1;0 Error is detected here Error is detected here Prefx Prefx
Background image of page 6
7 Error Recovery Strategies Panic mode – Discard input until a token in a set of designated synchronizing tokens is found Phrase-level recovery – Perform local correction on the input to repair the error Error productions – Augment grammar with productions for erroneous constructs Global correction – Choose a minimal sequence of changes to obtain a global least-cost correction
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Grammars (Recap) • Context-free grammar is a 4-tuple G = ( N , T , P , S ) where T is a Fnite set of tokens ( terminal symbols) N is a Fnite set of nonterminals P is a Fnite set of productions of the form ! " # where ! $ ( N % T )* N ( N % T )* and # $ ( N % T )* S $ N is a designated start symbol
Background image of page 8
9 Notational Conventions Used • Terminals a,b,c,… $ T specifc terminals: 0 , 1 , id , + • Nonterminals A,B,C,… $ N specifc nonterminals: expr , term , stmt • Grammar symbols X,Y,Z $ ( N % T ) • Strings oF terminals u,v,w,x,y,z $ T * • Strings oF grammar symbols ! , # , $ ( N % T )*
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

Ch4a - 1 ! Syntax Analysis" Part I! Chapter 4! COP5621...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online