This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 421 Lecture 7: Grammars and parsing s Announcements s MP2 review s Lecture outline s Contextfree grammars s Topdown, a.k.a. recursive descent, parsing 6/14/2009 1 Announcements s TA office hours s I2CS: Tue, Thu 45pm CDT s Oncampus: Wed 45pm CDT s MP2 solutions posted 6/14/2009 2 MP2 review s Problem 7 flatten : a list list > a list flatten [[1;2;3]; [4;5]; [8;2;3;4]];; let rec flatten lst = match lst with 6/14/2009 3 MP2 review s Problem 7 flatten : a list list > a list flatten [[1;2;3]; [4;5]; [8;2;3;4]];; let rec flatten lst = match lst with >  ::xs > flatten xs  (x::xs)::ys > x::(flatten (xs::ys));; 6/14/2009 4 Review: compiler frontend 6/14/2009 5 Tokens Source Lexer AST Parser Intro to grammars and languages s Grammar s Finite set of t e r m i n a l s s Finite set of n o n  t e r m i n a l s s Finite set of p r o d u c t i o n r u l e s s Start symbol s Language s Set of strings recognized by a grammar 6/14/2009 6 Grammars: Chomsky hierarchy s Unrestricted s Recursivelyenumerable languages s Recognized by a Turing machine s Contextsensitive s Contextsensitive languages s Recognized by a linear bounded automaton (LBA) s Contextfree s Contextfree languages s Recognized by a pushdown automaton (PDA) s Regular s Regular languages s Recognized by a finite state automaton (FSA) 6/14/2009 7 Contextfree grammar s Given: s Set of terminals (tokens) T s Set of nonterminals (variables) V s A cfg G is a set of p r o d u c t i o n s of the form s A X 1 X n ( n 0) where s A V , X 1 X n G = V T s One symbol designated as start symbol 6/14/2009 8 Notation s A X 1 X n s Also written A ::= X 1 X n s When n = 0, write A s Instead of A s When there is more than one production from A , say s A X 1 X n and A Y 1 Y n s Instead write: A X 1 X n  Y 1 Y n 6/14/2009 9 Example s Expressions s Exp intlit  variable  Exp + Exp  Exp * Exp s Sentences include s 3 s x s 3+x s 3+x*y s Tree representation 6/14/2009 10 Example s Method definition: MethodDef Type ident ( Args ) { Stmtlist } Args  NonEmptyArgs NonEmptyArgs Type ident  Type ident , NonEmptyArgs Stmtlist  Stmt Stmtlist Type ident  int  boolean s Sentence: int fun(boolean b) { } s Tree representation s ??...
View Full
Document
 Summer '09
 KirillMechitov

Click to edit the document details