Download Context-Free Grammars: Understanding Syntactically Correct Programs and more Study notes Computer Science in PDF only on Docsity! CS414-2008-02 Context-Free Grammars 1 02-0: Parsing โข Once we have broken an input file into a sequence of tokens, the next step is to determine if that sequence of tokens forms a syntactically correct program โ parsing โข We will use a tool to create a parser โ just like we used lex to create a parser โข We need a way to describe syntactically correct programs โข Context-Free Grammars 02-1: Context-Free Grammars โข Set of Terminals (tokens) โข Set of Non-Terminals โข Set of Rules, each of the form: <Non-Terminal> โ <Terminals & Non-Terminals> โข Special Non-Terminal โ Initial Symbol 02-2: Generating Strings with CFGs โข Start with the initial symbol โข Repeat: โข Pick any non-terminal in the string โข Replace that non-terminal with the right-hand side of some rule that has that non-terminal as a left-hand side Until all elements in the string are terminals 02-3: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num 02-4: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E 02-5: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E โ E + E CS414-2008-02 Context-Free Grammars 2 02-6: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E โ E + E โ E โ E + E 02-7: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E โ E + E โ E โ E + E โ num โE + E 02-8: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E โ E + E โ E โ E + E โ num โE + E โ num โ num +E 02-9: CFG Example E โ E + E E โ E โ E E โ E โ E E โ E/E E โ num E โ E + E โ E โ E + E โ num โE + E โ num โ num +E โ num โ num + num 02-10: CFG Example S โ NP V NP NP โthe N N โ boy N โ ball N โ window V โ threw V โ broke CS414-2008-02 Context-Free Grammars 5 E โ E + T E โ E โ T E โ T T โ T โ F T โ T/F T โ F F โ num F โ (E) 02-21: CFG for Statements โข Expressions: id, num โข Function calls: id(<input params>) โข <input params> are expressions separated by commas โข Block Statements { < list of statements > } โข While statements (C syntax) All statements are terminated by a semi-colon ; 02-22: CFG for Statements S โ id(P ); S โ {L} S โ while (E) S E โ id | num P โ วซ P โ EP โฒ P โฒ โ วซ P โฒ โ , EP โฒ L โ วซ L โ SL 02-23: Bakus Naur Form โข Another term for Context-Free grammars is Bakus Naur Form, or BNF โข We will use CFG and BNF interchangeably for this class 02-24: Extended Bakus Naur Form โข Use regular expression notation (*, +, |, ?) in BNF (CFG) rules (1) S โ { B } (2) S โ print (id) (3) B โ S ; C (4) C โ S ; C (5) C โ วซ โข Rules (3) - (5) describe 1 or more statements, terminated by ; 02-25: Extended Bakus Naur Form โข Use regular expression notation (*, +, |, ?) in BNF (CFG) rules (1) S โ { B } (2) S โ print โ(โ id โ)โ (3) B โ (S;)+ CS414-2008-02 Context-Free Grammars 6 โข Rules (3) describes 1 or more statements, terminated by ; 02-26: Extended Bakus Naur Form โข Pascal for statements: (1) S โ for id := E to E do S (2) S โ for id := E downto E do S 02-27: Extended Bakus Naur Form โข Pascal for statements: (1) S โ for id := E (to | downto) E do S โข Why this is useful (other than just reducing typing) will be seen when we generate parsers