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

Compiler Construction: Grammar Specification Language - Prof. Darko Stefanovic, Exams of Computer Science

The mid-term examination material for the compiler construction course, focusing on the grammar specification language. It includes the grammar rules for expressions and jw pascal programming language, as well as the terminal symbols and examples. Students are expected to understand the structure of the grammar and its application in compiler design.

Typology: Exams

Pre 2010

Uploaded on 07/22/2009

koofers-user-yzc
koofers-user-yzc 🇺🇸

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS
454/554 Compiler Construction, Fall 2001
1
Mid-term examination due Monday 15 October
This is a take-home examination. The goal is to learn the material on practical parsing, specifically LR(1) parsing, through the
implementation of an LR(1) parser generator.
Preparation
Read all the material in Chapter 2 of Scott, and in Chapter 3 of Cooper and Torczon (handout). You may also consult Aho,
Sethi, and Ullman or other sources listed in the course syllabus.
What should be built
Write a parser generator. The parser generator takes as input a formal description of a context-free grammar and produces as
output a parser for the language defined by the grammar.
The formal description of a context-free grammar is written in extended BNF notation defined below.
The generated parser is an LR(1) parser. Its action and goto tables should be constructed according to the procedure outlined
in Chapter 3 of Cooper and Torczon. The generated parser reads in a sequence of tokens. The generated parser produces the
output Yes or No. It does not need to build an explicit parse tree.
The generated parser may be in any programming language, but you must be able to compile, run, and test it. The generated
parser may be data-driven (a fixed skeleton parser consults explicit tables), or it may be code-driven (the tables are folded into
the control flow of the parser).
In case it is possible to produce an LR(1) parser for the input grammar, the states(item sets) and the final action and goto tables
should be printed in human-readable (symbolic) form.
In case it is not possible to produce an LR(1) parser for the input grammar, detailed error messages must also be printed to
describe the reasons of failure.
The parser generator should be tested on at least the grammars given below (which will also be provided on-line).
The parser generator may be in any programming language, but you must be able to compile, run, and test it.
Scanning
You may assume that scanning is done in some standard way, and the parser sees tokens returned by the scanner. You can glue
ad hoc scanners to the generated parsers, or you can even do the scanning by hand for your test inputs. If you wish, on the other
hand, you can integrate the parser generator with a scanner generator, and havea common syntax description file that specifies
both the context-free grammar and the several lexical classes of a language. Note that this may involve substantially more work.
Grammar specification language
The grammar of the language used to specify grammars is specified here in terms of itself.
1Grammar --> Symbols_List
2
3Symbols_List --> Symbol Symbols_List
4| Symbol
5
6Symbol --> Identifier Right_Arrow Productions_List
7
8Productions_List --> Production Productions_Separator Productions_List
9| Production
10
11 Production --> Identifiers_List
12
13 Identifiers_List --> Identifier Identifiers_List |
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Compiler Construction: Grammar Specification Language - Prof. Darko Stefanovic and more Exams Computer Science in PDF only on Docsity!

CS

454/554 Compiler Construction, Fall 2001 1

Mid-term examination — due Monday 15 October

This is a take-home examination. The goal is to learn the material on practical parsing, specifically LR(1) parsing, through the

implementation of an LR(1) parser generator.

Preparation

Read all the material in Chapter 2 of Scott, and in Chapter 3 of Cooper and Torczon (handout). You may also consult Aho,

Sethi, and Ullman or other sources listed in the course syllabus.

What should be built

Write a parser generator. The parser generator takes as input a formal description of a context-free grammar and produces as

output a parser for the language defined by the grammar.

The formal description of a context-free grammar is written in extended BNF notation defined below.

The generated parser is an LR(1) parser. Its action and goto tables should be constructed according to the procedure outlined

in Chapter 3 of Cooper and Torczon. The generated parser reads in a sequence of tokens. The generated parser produces the

output Yes or No. It does not need to build an explicit parse tree.

The generated parser may be in any programming language, but you must be able to compile, run, and test it. The generated

parser may be data-driven (a fixed skeleton parser consults explicit tables), or it may be code-driven (the tables are folded into

the control flow of the parser).

In case it is possible to produce an LR(1) parser for the input grammar, the states (item sets) and the final action and goto tables

should be printed in human-readable (symbolic) form.

In case it is not possible to produce an LR(1) parser for the input grammar, detailed error messages must also be printed to

describe the reasons of failure.

The parser generator should be tested on at least the grammars given below (which will also be provided on-line).

The parser generator may be in any programming language, but you must be able to compile, run, and test it.

Scanning

You may assume that scanning is done in some standard way, and the parser sees tokens returned by the scanner. You can glue

ad hoc scanners to the generated parsers, or you can even do the scanning by hand for your test inputs. If you wish, on the other

hand, you can integrate the parser generator with a scanner generator, and have a common syntax description file that specifies

both the context-free grammar and the several lexical classes of a language. Note that this may involve substantially more work.

Grammar specification language

The grammar of the language used to specify grammars is specified here in terms of itself.

1 Grammar --> Symbols_List

3 Symbols_List --> Symbol Symbols_List

4 | Symbol

6 Symbol --> Identifier Right_Arrow Productions_List

8 Productions_List --> Production Productions_Separator Productions_List

9 | Production

11 Production --> Identifiers_List

13 Identifiers_List --> Identifier Identifiers_List |

CS

454/554 Compiler Construction, Fall 2001 2

The terminal symbols in the grammar specification language are identifiers, the right arrow, and the production separator.

Identifiers are as in ModulaZ, or they can be an arbitrary string enclosed in a <...> pair. The right arrow is written --> or ::=,

and the production separator is written | or /.

Identifiers that appear on the left-hand side of right arrows are non-terminals. Any other identifiers are assumed to be terminals.

Analysis

Once you have built the parser generator, experiment with various grammars, including all that are given below. Report your

experiences. If a grammar does not permit the construction of an LR(1) parser, show how to modify the grammar (preserving

the same language!).

Example grammars

Expressions

1 Expr --> Expr plus Term 2 | Expr minus Term 3 | Term 4 Term --> Term times Factor 5 | Term divide Factor 6 | Factor 7 Factor --> lpar Expr rpar 8 | num 9 | id

JW Pascal

1 program --> program_heading block full_stop 2 3 program_heading --> program_keyword identifier 4 left_parenthesis file_identifier_list 5 right_parenthesis semicolon 6 7 file_identifier_list --> file_identifier comma 8 file_identifier_list | 9 file_identifier 10 11 file_identifier --> identifier 12 13 block --> label_declaration_part constant_definition_part 14 type_definition_part variable_declaration_part 15 procedure_and_function_declaration_part 16 statement_part 17 18 label_declaration_part --> label_keyword label_list semicolon | 19 20 21 label_list --> label comma label_list | 22 label 23 24 label --> unsigned_integer 25 26 constant_definition_part --> const_keyword 27 constant_definition_list 28 semicolon | 29 30 constant_definition_list --> constant_definition semicolon 31 constant_definition_list | 32 constant_definition

CS

454/554 Compiler Construction, Fall 2001 4

95 fixed_part | 96 variant_part | 97 98 fixed_part --> record_section_list 99 100 record_section_list --> record_section semicolon 101 record_section_list | 102 record_section 103 104 record_section --> field_identifier_list colon type 105 106 field_identifier_list --> field_identifier comma 107 field_identifier_list | 108 field_identifier 109 110 field_identifier --> identifier 111 112 variant_part --> case_keyword tag_field type_identifier 113 of_keyword variant_list 114 115 variant_list --> variant semicolon variant_list | 116 variant 117 118 tag_field --> field_identifier colon | 119 120 121 variant --> case_label_list colon left_parenthesis 122 field_list right_parenthesis 123 124 case_label_list --> case_label comma case_label_list | 125 case_label 126 127 case_label --> constant 128 129 set_type --> set_keyword of_keyword base_type 130 131 base_type --> simple_type 132 133 file_type --> file_keyword of_keyword type 134 135 pointer_type --> up_arrow type_identifier 136 137 variable_declaration_part --> var_keyword 138 variable_declaration_list 139 semicolon | 140 141 variable_declaration_list --> variable_declaration semicolon 142 variable_declaration_list | 143 variable_declaration 144 145 variable_declaration --> identifier_list colon type 146 147 procedure_and_function_declaration_part --> 148 procedure_or_function_declaration_list 149 150 procedure_or_function_declaration_list --> 151 procedure_or_function_declaration semicolon 152 procedure_or_function_declaration_list | 153 154 procedure_or_function_declaration --> procedure_declaration | 155 function_declaration 156

CS

454/554 Compiler Construction, Fall 2001 5

157 procedure_declaration --> procedure_heading block 158 159 procedure_heading --> procedure_keyword identifier semicolon | 160 procedure_keyword identifier 161 left_parenthesis 162 formal_parameter_section_list 163 right_parenthesis semicolon 164 165 formal_parameter_section_list --> formal_parameter_section 166 semicolon 167 formal_parameter_section_list | 168 formal_parameter_section 169 170 formal_parameter_section --> parameter_group | 171 var_keyword parameter_group | 172 function_keyword parameter_group | 173 procedure_keyword 174 identifier_list 175 176 parameter_group --> identifier_list colon type_identifier 177 178 function_declaration --> function_heading block 179 180 function_heading --> function_keyword identifier colon 181 result_type semicolon | 182 function_keyword identifier 183 left_parenthesis 184 formal_parameter_section_list 185 right_parenthesis 186 colon result_type semicolon 187 188 result_type --> type_identifier 189 190 statement_part --> compound_statement 191 192 statement --> label colon unlabelled_statement | 193 unlabelled_statement 194 195 unlabelled_statement --> structured_statement | 196 simple_statement 197 198 simple_statement --> assignment_statement | 199 procedure_statement | 200 go_to_statement | 201 empty_statement 202 203 assignment_statement --> variable assignment_sign expression | 204 function_identifier assignment_sign 205 expression 206 207 variable --> entire_variable specifier_list | 208 entire_variable 209 210 entire_variable --> identifier 211 212 specifier_list --> specifier specifier_list | 213 specifier 214 215 specifier --> up_arrow | 216 record_field | 217 array_index 218

CS

454/554 Compiler Construction, Fall 2001 7

282 procedure_identifier --> identifier 283 284 actual_parameter_list --> actual_parameter comma 285 actual_parameter_list | 286 actual_parameter 287 288 actual_parameter --> expression | 289 procedure_identifier | 290 function_identifier 291 292 go_to_statement --> goto_keyword label 293 294 empty_statement --> 295 296 structured_statement --> compound_statement | 297 conditional_statement | 298 repetitive_statement | 299 with_statement 300 301 compound_statement --> begin_keyword statement_list 302 end_keyword 303 304 conditional_statement --> if_statement | 305 case_statement 306 307 if_statement --> if_keyword expression then_keyword 308 statement else_keyword statement | 309 if_keyword expression then_keyword 310 statement 311 312 case_statement --> case_keyword expression of_keyword 313 case_list end_keyword 314 315 case_list --> case_list_element semicolon case_list | 316 case_list_element 317 318 case_list_element --> case_label_list colon statement | 319 320 repetitive_statement --> while_statement | 321 repeat_statement | 322 for_statement 323 324 while_statement --> while_keyword expression do_keyword 325 statement 326 327 repeat_statement --> repeat_keyword statement_list 328 until_keyword expression 329 330 statement_list --> statement semicolon statement_list | 331 statement 332 333 for_statement --> for_keyword control_variable 334 assignment_sign for_list do_keyword 335 statement 336 337 for_list --> initial_value to_keyword final_value | 338 initial_value downto_keyword final_value 339 340 control_variable --> identifier 341 342 initial_value --> expression

CS

454/554 Compiler Construction, Fall 2001 8

344 final_value --> expression 345 346 with_statement --> with_keyword record_variable_list 347 do_keyword statement 348 349 record_variable_list --> variable comma 350 record_variable_list | 351 variable 352 353 sign --> plus_sign | 354 minus_sign

ModulaZ

1 Program 2 --> Module 3 4 Module 5 --> module identifier 6 semi BlockNoScope identifier dot 7 8 Block 9 --> 10 BlockNoScope 11 12 BlockNoScope 13 --> OptDecls begin Stmts end 14 15 OptDecls 16 --> 17 | Decls 18 19 Decls 20 --> Declaration 21 | Decls Declaration 22 23 Declaration 24 --> const OptConstDecls 25 | type OptTypeDecls 26 | typerec RecTypeDecls 27 | var OptVarDecls 28 | ProcDecl 29 30 RecTypeDecls 31 --> TypeDecl 32 | RecTypeDecls and TypeDecl 33 34 OptConstDecls 35 --> 36 | ConstDecls 37 38 OptTypeDecls 39 --> 40 | TypeDecls 41 42 OptVarDecls 43 --> 44 | VarDecls 45 46 ConstDecls

CS

454/554 Compiler Construction, Fall 2001 10

109 | AssignStmt 110 | CallStmt 111 | ExitStmt 112 | EvalStmt 113 | ForStmt 114 | IfStmt 115 | LoopStmt 116 | ReadStmt 117 | RepeatStmt 118 | ReturnStmt 119 | WhileStmt 120 | WriteStmt 121 122 AssignStmt 123 --> Expr coloneq Expr 124 125 CallStmt 126 --> ProcCall 127 128 ExitStmt 129 --> exit 130 131 EvalStmt 132 --> eval Expr 133 134 ForStmt 135 --> for identifier coloneq Expr to Expr OptBy do 136 Stmts end 137 138 OptBy 139 --> 140 | by Expr 141 142 IfStmt 143 --> IfHead end 144 | IfHead else 145 Stmts end 146 147 IfHead 148 --> if Expr then 149 Stmts 150 | IfHead elsif 151 Expr then 152 Stmts 153 154 LoopStmt 155 --> loop 156 Stmts end 157 158 ReadStmt 159 --> read lpar text comma ReadList rpar 160 161 ReadList 162 --> Expr 163 | Expr comma ReadList 164 165 RepeatStmt 166 --> repeat 167 Stmts until Expr 168 169 ReturnStmt 170 --> return

CS

454/554 Compiler Construction, Fall 2001 11

171 | return Expr 172 173 WhileStmt 174 --> while 175 Expr do 176 Stmts end 177 178 WriteStmt 179 --> write lpar text WriteList rpar 180 181 WriteList 182 --> 183 | comma Expr WriteList 184 185 Type 186 --> ArrayType 187 | RecordType 188 | SubrangeType 189 | identifier 190 | lpar Type rpar 191 | ref Type 192 193 ArrayType 194 --> array OptTypeList of Type 195 196 OptTypeList 197 --> 198 | TypeList 199 200 TypeList 201 --> Type 202 | TypeList comma Type 203 204 RecordType 205 --> record OptFields end 206 207 OptFields 208 --> 209 | Fields 210 | Fields semi 211 212 Fields 213 --> IDList colon Type 214 | Fields semi IDList colon Type 215 216 SubrangeType 217 --> lbrack Expr dotdot Expr rbrack 218 219 Expr 220 --> Expr or Expr 221 | Expr and Expr 222 | not Expr 223 | Expr Relop Expr 224 | Expr Addop Expr 225 | Expr Mulop Expr 226 | Sign Expr 227 | Primary 228 229 Relop 230 --> equal 231 | nequal 232 | less