Question

In: Computer Science

The following grammar for a program has a problem with semicolons: Program ::= Block "." Block...

The following grammar for a program has a problem with semicolons:

  • Program ::= Block "."
  • Block ::= "{" Statement (";" Statement )* "}"
  • Statement ::= Assignment | IfStatement | Block
  • Assignment ::= "="
  • IfStatement ::= "if" "(" Assignment ")" Statement

Use JavaCC to update

When an if statement has a block after it, the block sometimes must end with a semicolon sometimes but not all the time.

  1. Write an example showing a required semicolon after a "}"
  2. Write an example where a semicolon is not required after a "}"
  3. How can the grammar be modified so that several semicolons can occur in a row with not statements.
  4. Can the grammar be modified so that a semicolon is not required after a "}" that ends a Block? Rewrite the grammar to make a semicolon not be required after a "}".

Solutions

Expert Solution

The Main purpose of semicolen here is to separate independent statements.

Example : Statement 1 : statement 2 : statement 3;

semicolens are used where the comma and full stops can not be used and are inappropriate.

Javacc 1 .A program that creates parsers

2.The source code(input) is a definition file ◦ Grammar definition ◦ Inline Java code

3 The target(output) is java-based parser

BASICS of JavaCC need to know for better understanding of program

javacc_input ::= javacc_options

                 "PARSER_BEGIN" "(" identifier ")"

                 CompilationUnit

                 "PARSER_END" "(" identifier ")"

                 ( production )+

                 <EOF>

javacc_options ::= ( <IDENTIFIER> "{" ( option_binding )* "}" )?

option_binding ::= ( <IDENTIFIER> | "LOOKAHEAD" | "IGNORE_CASE" | "static" | "PARSER_BEGIN" )

                   "="

                   ( IntegerLiteral | BooleanLiteral | StringLiteral | StringList )

                   ";"

option_binding ::= ( <IDENTIFIER> | "LOOKAHEAD" | "IGNORE_CASE" | "static" | "PARSER_BEGIN" )

                   "="

                   ( IntegerLiteral | BooleanLiteral | StringLiteral | StringList )

                   ";"

StringList ::= "(" StringLiteral ( "," StringLiteral )* ")"

production ::= javacode_production

             | cppcode_production

             | regular_expr_production

             | token_manager_decls

             | bnf_production

javacode_production ::= "JAVACODE"

                        AccessModifier

                        ResultType

                        identifier

                        FormalParameters ( "throws" Name ( "," Name )* )?

                        Block

cppcode_production ::= "CPPCODE"

                       AccessModifier

                       ResultType

                       identifier

                       FormalParameters ( "throws" Name ( "," Name )* )?

                       Block

bnf_production ::= AccessModifier

                   ResultType

                   identifier

                   FormalParameters ( "throws" Name ( "," Name )* )?

                   ":"

                   Block

                   "{" expansion_choices "}"

AccessModifier ::= ( "public" | "protected" | "private" )?

regular_expr_production ::= ( "<" "*" ">"

                            | "<" <IDENTIFIER> ( "," <IDENTIFIER> )* ">"

                            )?                    

                            regexpr_kind ( "[" "IGNORE_CASE" "]" )? ":" "{"

                            regexpr_spec ( "|" regexpr_spec )* "}"

token_manager_decls ::= "TOKEN_MGR_DECLS" ":" ( ClassOrInterfaceBody )?

regexpr_kind ::= "TOKEN"

               | "SPECIAL_TOKEN"

               | "SKIP"

               | "MORE"

regexpr_spec ::= regular_expression

                 ( Block )?

                 ( ":" <IDENTIFIER> )?

expansion_choices ::= expansion ( "|" expansion )*

expansion ::= ( "LOOKAHEAD" "(" local_lookahead ")" )?

              ( expansion_unit )+

local_lookahead ::= ( IntegerLiteral )?

                    ( "," )?

                    ( expansion_choices )?

                    ( "," )?

                    ( "{" ( Expression )? "}" )?

expansion_unit ::= "LOOKAHEAD" "(" local_lookahead ")"

                 | Block

                 | "[" expansion_choices "]"

                 | "try" "{" expansion_choices "}"

                 ( "catch" "(" ( Name <IDENTIFIER> )? ")" Block )*

                 ( "finally" Block )?

                 | ( PrimaryExpression "=" )?

                 (

                   identifier ( TypeArguments )? Arguments

                 | regular_expression ( "." <IDENTIFIER> )?

                 )

                 | "(" expansion_choices ")"

                 ( "+" | "*" | "?" )?

regular_expression ::= StringLiteral

                     | <LANGLE: "<">

                       ( ( "#" )? identifier ":" )?

                       complex_regular_expression_choices <RANGLE: ">">

                     | "<" identifier ">"

                     | "<" "EOF" ">"

complex_regular_expression_choices ::= complex_regular_expression

                                       ( "|" complex_regular_expression )*

complex_regular_expression ::= ( complex_regular_expression_unit )+

complex_regular_expression_unit ::= StringLiteral

                                  | "<" identifier ">"

                                  | character_list

                                | "(" complex_regular_expression_choices ")"

                                  (

                                    "+" | "*" | "?" | "{"

                                    IntegerLiteral

                                    ( "," ( IntegerLiteral )? )?

                                    "}"

                                  )?

character_list ::= ( "~" )? "[" ( character_descriptor ( "," character_descriptor )* )? "]"

character_descriptor ::= StringLiteral ( "-" StringLiteral )?

identifier ::= <IDENTIFIER>

Program structure

CompilationUnit ::= ( PackageDeclaration )?

                    ( ImportDeclaration )*

                    ( TypeDeclaration )*

PackageDeclaration ::= Modifiers "package" Name ";"

ImportDeclaration ::= "import"

                      ( "static" )?

                      Name

                      ( "." "*" )?

                      ";"

Example Program :

PARSER_BEGIN(Example)

/**

* Simple brace matcher.

*/

public class Example{

/** Main entry point. */

public static void main(String args[]) throws ParseException {

    Example3 parser = new Example(System.in);

    parser.Input();

}

}

PARSER_END(Example)

SKIP :

{

" "

| "\t"

| "\n"

| "\r"

}

TOKEN :

{

<LBRACE: "{">

| <RBRACE: "}">

}

/** Root production. */

void Input() :

{ int count; }

{

count=MatchedBraces() <EOF>

{ System.out.println("The levels of nesting is " + count); }

}

/** Brace counting production. */

int MatchedBraces() :

{ int nested_count=0; }

{

<LBRACE> [ nested_count=MatchedBraces() ] <RBRACE>

{ return ++nested_count; }

}


Related Solutions

nterpret the following NC program block by block. Indicate G and M functions for each block,...
nterpret the following NC program block by block. Indicate G and M functions for each block, describe the tool movement and its path also. N010 G90 G70 M03 S1200 T05 N020 G00 X0.375 Y0.875 N030 Z0.1 N040 G01 Z-0.1 F10.0 M08 N050 X2.0 F20.0 N060 G02 X2.375 Y0.5 I0.0 J-0.375 N070 G01 Y0.375 N080 X3.625 N090 Y1.25 N100 X3.5 N110 G02 Y1.875 I0.0 J0.375 N120 G01 X3.625 N130 Y2.625 N140 X2.375
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
Problem 1. Network Address Suppose an ISP has the following IP address block: 130.12.128.0/23 a. Assume...
Problem 1. Network Address Suppose an ISP has the following IP address block: 130.12.128.0/23 a. Assume that the ISP wants to divide its address block (130.12.128.0/23) into two equal-sized contiguous address blocks, and give one of these address blocks to its customers (C1 and C2). Provide network addresses (of the form a.b.c.d/x) for C1 and C2. b. Now let’s assume that the ISP wants to divide its address block (130.12.128.0/23) into four equal-sized contiguous address blocks, and give one of...
A composition teacher wishes to see whether a new grammar program will reduce the number of...
A composition teacher wishes to see whether a new grammar program will reduce the number of grammatical errors her student make when writing an essay. the data are shown below. At a=0.05 can it be concluded that the number of errors has been reduced? Student- 1, 2, 3, 4, 5, 6, Errors before- 12 9 0 5 4 3 Errors after - 9 6 1 1 2 5
A composition teacher wishes to see whether a new grammar program will reduce the number of...
A composition teacher wishes to see whether a new grammar program will reduce the number of grammatical errors her students make when writing a two-page essay. The data are shown here. Student 1 2 3 4 5 6 Errors before 12 9 0 5 4 3 Errors after 9 6 1 3 2 3 a. To find the 95% confidence interval for μd., what critical value should be used? b. Find the 95% confidence interval for μd.
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function...
Program in Python Problem Statement Write a program with the following functions:  wordCount. This function should accept a string as a parameter and return the number of words contained in the string.  mostFrequentWord. This function accepts a string as a parameter and returns the word that occurs the most frequently in the string.  replaceWord. This function accepts three strings as parameters, let’s call them string1, string2, and string3. It searches string1 for all occurrences of string2. When...
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Block A in the figure below has mass 1.30 kg , and block B has mass...
Block A in the figure below has mass 1.30 kg , and block B has mass 2.85 kg . The blocks are forced together, compressing a spring S between them; then the system is released from rest on a level, frictionless surface. The spring, which has negligible mass, is not fastened to either block and drops to the surface after it has expanded. Block B acquires a speed of 1.20 m/s . Part A What is the final speed of...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT