Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Context-Free Grammars: Understanding Syntactically Correct Programs, Study notes of Computer Science

Context-free grammars (cfgs), a way to describe syntactically correct programs. Cfgs consist of terminals (tokens), non-terminals, rules, and an initial symbol. The process of generating strings with cfgs involves starting with the initial symbol and repeatedly replacing non-terminals with the right-hand side of rules. Examples of cfgs and their derivations, as well as the concept of parse trees and their relationship to meaning and ambiguity.

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-0jz
koofers-user-0jz ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

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
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved