Pearson eText for Concepts of Programming Languages -- Instant Access (Pearson+)
12th Edition
ISBN: 9780135102268
Author: Robert Sebesta
Publisher: PEARSON+
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 3, Problem 9PS
Explanation of Solution
Consider the following grammar in BNF:
<assign> -> <id> = <expr>
<id> —> A|B|C
<expr> -> <expr> + <term> | <term>
<term> -> <term> * <factor> | <factor>
<factor> -> (<expr>)| <id >
The grammar of Example 3.4 to add a unary minus operator that has higher precedence than either + or * are as follows:
<assign> -> <id> = <expr>
<id> —> A|B|C
<expr> -> <expr> + <term> | <term>
<term> -> <term> * <factor> | <factor>
...Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a GO grammar expression where:
1. An expression is either an id (you may assume this token has been defined) or a combination of subexpressions using the **, ^ and $ operators
2. ** is a binary infix operator, while ^ is a unary operator that appears before an expression and $ is an operator that appears after an expression
3. ^ has precedence over **, which has precedence over $
4. ** has right-associativity
1. Use the algorithm described in class that takes a CFG and converts it into a CNF (Chomsky Normal Form) grammar that generates the same language as the original grammar, except possibly for the string A, to answer the following question. As part of your answer, you should say which variables are nullable, and you should give the appropriate grammar after each step of the algorithm. There are four steps.
Give a CFG (context-free grammar) in CNF (Chomsky Normal Form) that generates the same language, except possibly for the string A, as the following grammar:
SAB AB →bAa A → aaB bb
Use the grammar you have created in Question 1 for an infix expression and using the technique: Syntax Directed Definition write appropriate semantic rules to convert an infixexpression of that grammar to a prefix expression.Precedence of operators are given below (high to low): ()*, / +, - =Associativity of operators are given below:+, , *, / [Left Associative]= [Right Associative]Taking into account the precedence and the associativity of the operators in this question, if any modification is needed for your grammar in Question 1 to be used in this question, do it. After the necessary modification(s), a) write the semantic rules for infix to prefix conversionb) draw the annotated parse tree for the string: a=b/(c–d)*(x+y)
Chapter 3 Solutions
Pearson eText for Concepts of Programming Languages -- Instant Access (Pearson+)
Ch. 3 - Prob. 1RQCh. 3 - Prob. 2RQCh. 3 - Prob. 3RQCh. 3 - Prob. 4RQCh. 3 - Prob. 5RQCh. 3 - Prob. 6RQCh. 3 - Prob. 7RQCh. 3 - Prob. 8RQCh. 3 - Prob. 9RQCh. 3 - What is the difference between a synthesized and...
Ch. 3 - Prob. 11RQCh. 3 - Prob. 12RQCh. 3 - Prob. 13RQCh. 3 - Prob. 14RQCh. 3 - Prob. 15RQCh. 3 - Prob. 16RQCh. 3 - Prob. 17RQCh. 3 - Prob. 18RQCh. 3 - Prob. 19RQCh. 3 - Prob. 20RQCh. 3 - Prob. 21RQCh. 3 - What does partial correctness mean for a loop...Ch. 3 - Prob. 23RQCh. 3 - Prob. 24RQCh. 3 - Prob. 25RQCh. 3 - Prob. 26RQCh. 3 - Prob. 27RQCh. 3 - Prob. 28RQCh. 3 - Prob. 29RQCh. 3 - The two mathematical models for language...Ch. 3 - Write EBNF descriptions for the following: a. A...Ch. 3 - Prob. 3PSCh. 3 - Prob. 4PSCh. 3 - Prob. 5PSCh. 3 - Prob. 6PSCh. 3 - Prob. 9PSCh. 3 - Prob. 10PSCh. 3 - Prob. 12PSCh. 3 - Prob. 15PSCh. 3 - Prob. 16PSCh. 3 - Prob. 17PSCh. 3 - Prob. 18PSCh. 3 - Compute the weakest precondition for each of the...
Knowledge Booster
Similar questions
- For this exercise, we will work on a regular grammar to recognize a comment in a C++ or Java program. A comment starts with /* and ends with */ and anything else in between can be ignored. We won't consider the case of nested comments. Keep in mind that you can have any number of * characters at the beginning, at the end, and in between. • Write a regular grammar recognizing such comments. • Draw the finite state machine for this grammar. • Draw a table for this grammar such as the ones for table-driver scanners in the class notes and in the book.arrow_forwardContext free grammararrow_forwardConvert the grammar into Greibach normal form. In addition, please show what you did to get the answer on paper or by typing it. Grammar:S →aSb | ab | bbarrow_forward
- Write a grammar for Boolean expressions , assuming that we already have rules in our grammar for arithmetic expressions . Arithmetic expressions might be simple literals like 3 or 97 or may be more complex syntactic units like (3+x)/4. They evaluate to a number. You needn't concern yourself with their structure; just that they can be used as below. Boolean expressions include the literals TRUE and FALSE; they can be combined with the unary operator ! (meaning NOT) and binary operators && and || (meaning AND and OR). Also, arithmetic expressions can be combined with the following binary operators to form new Boolean expressions: , =, !=. When your grammar is complete, you should be able to derive parse trees for expressions like the following: TRUE || FALSE FALSE && !TRUE 27 = TRUE | FALSEarrow_forwardWrite a grammar for the following language. This language consists of strings that have n copies of the letter b preceded by n * 2 copies of the letter a, where n is an odd integer > 0. For example, the strings aab, aaaaaabbb, aaaaaaaaaabbbbb are in the language and the strings a, ab, aaaabb (because there are not an odd number of b's), b, ba are notarrow_forward3. Write a correct recursive descent grammar that correctly recognizes arithmetic expressions involving variable, number, + - * / **. Variable and number are tokens recognized by the lexical analyzer along with the operators + - * / and **. There are three levels of hierarchy, ** before *, / before + -. This grammar does not support unary + and -.arrow_forward
- Problem 1: Write a regular expression that recognizes the following language over the alphabet {a,b}: Any string containing the letters a and b, where the number of letters is not a multiple of three. (Note that 0 is a multiple of three.) Your regular expression should contain only a and b, and the metacharacters (, ), *, +, ? and |. Problem 2: Write a context free grammar that recognizes language from Problem 1. Your start symbol should be named S.arrow_forward@ obtain Regular Expression from the grammar given below S---> bas| aA A--->bbA|bbarrow_forwarda. Eliminate Left Factoring from the grammar above. b. Eliminate Left Recursive from the grammar above if there is any.arrow_forward
- Eliminate left recursion in the following grammar.arrow_forwardConsider the following grammar: S➞ ACB/CbB/BaA➞ da/BCB➞ g/∈C➞ h/∈ What is the follow of B?arrow_forwardUse the logic in the program below to design a Recursive Descent parser in Cfor the following grammar:S →aABA →Abc | bB →d The program below is designed to parse the grammar below. Use the program below as a template and rewrite it so it can parse the grammar above /*Recursive Descent Parser for the expression grammar S -> (L) | a L' -> ,SL | ε L -> SL' */ #include <stdio.h>#include <string.h>int S(), Ldash(), L();char *ip;char string[50];int main() {printf("Enter the string\n");scanf("%s", string);ip = string;printf("\n\nInput\t\tAction\n ------------------------------\n");if (S()){printf("\n------------------------------------------------\n");printf("\n String is successfully parsed\n");}else{printf("\n ------------------------------------------------\n");printf("Error in parsing string\n");}}int L(){printf("%s\t\tL →SL' \n", ip);if (S()){if (Ldash()){return 1;}elsereturn 0;}elsereturn 0;}int Ldash(){if (*ip == ','){printf("%s\t\tL' →, SL' \n", ip);ip++;if…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education