1) 10 points Convert the following context-free grammar to attributes grammar in order to recognize strings that consist of n number of “a” followed by n number of “b” followed by n number of “c,”...

1 answer below »
do you know how to solve this assignment, computer programming language


1) 10 points Convert the following context-free grammar to attributes grammar in order to recognize strings that consist of n number of “a” followed by n number of “b” followed by n number of “c,” where n >= 1. The strings “aaabbbccc” and “abc” belong to this grammar, but the strings “aaabbbbcc” and “aabbbcc” do not. ::= ::= a | a ::= b | b ::= c | c 2) 5 points What is the weak precondition for the following if-else statement? { P } if ( x > y ) y = 2 x + 1 else y = 3 x − 1 { y > 3} 3) 10 points Prove Total Correctness while ( n != 1 ) { if ( n % 2 == 0 ){ n = n/ 2; } else { n = 3n + 1; } } { n = 1 } 4) 10 points Prove Total Correctness n = n0; m = m0; while (n != 0) { if {n < m) { m = m – n; } else { int temp = m; m = n; n = temp; } } { m = gcd(m,n)} 5) 15 points modify the lexical analyzer from your homework 2 to recognize the following list of reserved words and return their respective token codes: for, if, else, while, do, int, float, switch, class, int, void, bool. 6) 15 points modify the syntax analyzer from your homework 2 to add a subprogram for while-statements, if-then-else- statements, and do-while-statements that would recognize the corresponding statements in java or c++, if any special symbols are required make sure you add them to your lexical analyzer. 7) 10 points describe the process in creating a symbol table for a programming language that you are creating that allows for identifiers, which can be variables or function names. each function has its own local variables, and wo do code blocks for selection statements (if, switch), loops (while, for, do), and lambda functions (unnamed inline functions). this language you are defined should use static scoping, should hide nonlocal variables that’s names have been reused. detail any data structures that would be used in this scenario, and state exactly how they would be used and added to. what stage of the compilation process would it be adding new identifiers to it. 8) 10 points consider the following python program: x = 1; y = 3; z = 5; def sub1(): a = 7; y = 9; z = 11; # . . . line 1 def sub2(): global x; a = 13; x = 15; w = 17; # . . . line 2 def sub3(): nonlocal a; a = 19; b = 21; z = 23; # . . . line 3 def sub2(): nonlocal a; global z; a = 19; b = 21; z = 23; # . . . line 4 . . . # . . . line 5 list all the variables, and functions along with the program units where they are declared, that are visible and hidden in the bodies of every subroutine. assume static scoping is used. state which identifiers are hidden*** 9) 10 points assume the problem above is using dynamic scoping now. detail what you would need to add or change in the code to really show the proper flow of the dynamic scoping rules. ******* extra credit replace one grade (not final) . pre-requirement detail the mistakes made on homework 1, homework 2, and test 1. write what problems you missed and why that answer is incorrect. assignment edit and improve homework 2 by adding a symbol table. to do this you are required to add the concept of variable type declaration, and assignment operations. this will require the additional declaration statement to your rule base as well as an assignment statement. you are also required to add the proper keyword for integer and floating point declaration. your symbol table should store the name of the identifier, the data type of the variable, and the position of the variable. the position of the variable is considered the number lexeme it was defined as. homework 2   previous next  instructions in this homework assignment you will be creating the lexical and syntax analyzer for a programming language that will be defined in this assignment. this language will be able to calculate basic mathematic operations and relational operations for integers and floating point numbers, as well as variables that can be either.   this program should be able to not only recognize the following operations but have a proper evaluation order that conforms to the real life principles of mathematics for in order operations: · addition · subtraction · multiplication · division · module · less than · greater than · equal to · not equal to · assignment (there must also be a way to break precedence, this is usually done with the use of parenthesis) each program should have a clear beginning and end as well as a way to separate multiple statements.   your assignment is 4 fold: 1. define the rules for recognizing all lexemes as their proper token 2. define production rules for implementing the mathematical syntax of  operators and operands a. make sure it conforms to the standard of an ll grammar b. make sure it properly enforces the correct precedence rules c. make sure it is not ambiguous in creating parses 3. write a program that recognizes all tokens in a file 4. write a program or an extension to the above program that determines if the tokens conform to the correct syntax. m)="" {="" m="m" –="" n;="" }="" else="" {="" int="" temp="m;" m="n;" n="temp;" }="" }="" {="" m="gcd(m,n)}" 5)="" 15="" points="" modify="" the="" lexical="" analyzer="" from="" your="" homework="" 2="" to="" recognize="" the="" following="" list="" of="" reserved="" words="" and="" return="" their="" respective="" token="" codes:="" for,="" if,="" else,="" while,="" do,="" int,="" float,="" switch,="" class,="" int,="" void,="" bool.="" 6)="" 15="" points="" modify="" the="" syntax="" analyzer="" from="" your="" homework="" 2="" to="" add="" a="" subprogram="" for="" while-statements,="" if-then-else-="" statements,="" and="" do-while-statements="" that="" would="" recognize="" the="" corresponding="" statements="" in="" java="" or="" c++,="" if="" any="" special="" symbols="" are="" required="" make="" sure="" you="" add="" them="" to="" your="" lexical="" analyzer.="" 7)="" 10="" points="" describe="" the="" process="" in="" creating="" a="" symbol="" table="" for="" a="" programming="" language="" that="" you="" are="" creating="" that="" allows="" for="" identifiers,="" which="" can="" be="" variables="" or="" function="" names.="" each="" function="" has="" its="" own="" local="" variables,="" and="" wo="" do="" code="" blocks="" for="" selection="" statements="" (if,="" switch),="" loops="" (while,="" for,="" do),="" and="" lambda="" functions="" (unnamed="" inline="" functions).="" this="" language="" you="" are="" defined="" should="" use="" static="" scoping,="" should="" hide="" nonlocal="" variables="" that’s="" names="" have="" been="" reused.="" detail="" any="" data="" structures="" that="" would="" be="" used="" in="" this="" scenario,="" and="" state="" exactly="" how="" they="" would="" be="" used="" and="" added="" to.="" what="" stage="" of="" the="" compilation="" process="" would="" it="" be="" adding="" new="" identifiers="" to="" it.="" 8)="" 10="" points="" consider="" the="" following="" python="" program:="" x="1;" y="3;" z="5;" def="" sub1():="" a="7;" y="9;" z="11;" #="" .="" .="" .="" line="" 1="" def="" sub2():="" global="" x;="" a="13;" x="15;" w="17;" #="" .="" .="" .="" line="" 2="" def="" sub3():="" nonlocal="" a;="" a="19;" b="21;" z="23;" #="" .="" .="" .="" line="" 3="" def="" sub2():="" nonlocal="" a;="" global="" z;="" a="19;" b="21;" z="23;" #="" .="" .="" .="" line="" 4="" .="" .="" .="" #="" .="" .="" .="" line="" 5="" list="" all="" the="" variables,="" and="" functions="" along="" with="" the="" program="" units="" where="" they="" are="" declared,="" that="" are="" visible="" and="" hidden="" in="" the="" bodies="" of="" every="" subroutine.="" assume="" static="" scoping="" is="" used.="" state="" which="" identifiers="" are="" hidden***="" 9)="" 10="" points="" assume="" the="" problem="" above="" is="" using="" dynamic="" scoping="" now.="" detail="" what="" you="" would="" need="" to="" add="" or="" change="" in="" the="" code="" to="" really="" show="" the="" proper="" flow="" of="" the="" dynamic="" scoping="" rules.="" *******="" extra="" credit="" replace="" one="" grade="" (not="" final)="" .="" pre-requirement="" detail="" the="" mistakes="" made="" on="" homework="" 1,="" homework="" 2,="" and="" test="" 1.="" write="" what="" problems="" you="" missed="" and="" why="" that="" answer="" is="" incorrect.="" assignment="" edit="" and="" improve="" homework="" 2="" by="" adding="" a="" symbol="" table.="" to="" do="" this="" you="" are="" required="" to="" add="" the="" concept="" of="" variable="" type="" declaration,="" and="" assignment="" operations.="" this="" will="" require="" the="" additional="" declaration="" statement="" to="" your="" rule="" base="" as="" well="" as="" an="" assignment="" statement.="" you="" are="" also="" required="" to="" add="" the="" proper="" keyword="" for="" integer="" and="" floating="" point="" declaration.="" your="" symbol="" table="" should="" store="" the="" name="" of="" the="" identifier,="" the="" data="" type="" of="" the="" variable,="" and="" the="" position="" of="" the="" variable.="" the="" position="" of="" the="" variable="" is="" considered="" the="" number="" lexeme="" it="" was="" defined="" as.="" homework="" 2=""  ="" previous next ="" instructions="" in="" this="" homework="" assignment="" you="" will="" be="" creating="" the="" lexical="" and="" syntax="" analyzer="" for="" a="" programming="" language="" that="" will="" be="" defined="" in="" this="" assignment.="" this="" language="" will="" be="" able="" to="" calculate="" basic="" mathematic="" operations="" and="" relational="" operations="" for integers and floating="" point numbers,="" as="" well="" as variables that="" can="" be="" either.=""  ="" this="" program="" should="" be="" able="" to="" not="" only="" recognize="" the="" following operations but="" have="" a="" proper evaluation="" order that="" conforms="" to="" the="" real="" life="" principles="" of="" mathematics="" for="" in="" order="" operations:="" ·="" addition="" ·="" subtraction="" ·="" multiplication="" ·="" division="" ·="" module="" ·="" less="" than="" ·="" greater="" than="" ·="" equal="" to="" ·="" not="" equal="" to="" ·="" assignment="" (there="" must="" also="" be="" a="" way="" to="" break="" precedence,="" this="" is="" usually="" done="" with="" the="" use="" of="" parenthesis)="" each="" program="" should="" have="" a="" clear="" beginning="" and="" end="" as="" well="" as="" a="" way="" to="" separate="" multiple="" statements.=""  ="" your="" assignment="" is="" 4="" fold:="" 1.="" define="" the="" rules="" for="" recognizing="" all="" lexemes="" as="" their="" proper="" token="" 2.="" define="" production="" rules="" for="" implementing="" the="" mathematical="" syntax="" of=""  operators="" and="" operands="" a.="" make="" sure="" it="" conforms="" to="" the="" standard="" of="" an ll="" grammar="" b.="" make="" sure="" it="" properly="" enforces="" the="" correct precedence="" rules="" c.="" make="" sure="" it="" is="" not="" ambiguous="" in="" creating="" parses="" 3.="" write="" a="" program="" that="" recognizes="" all="" tokens="" in="" a="" file="" 4.="" write="" a="" program="" or="" an="" extension="" to="" the="" above="" program="" that="" determines="" if="" the="" tokens="" conform="" to="" the="" correct="">
Answered Same DayJul 27, 2021

Answer To: 1) 10 points Convert the following context-free grammar to attributes grammar in order to recognize...

Karthi answered on Jul 28 2021
142 Votes
1.
import string
bad_chars = [';', ':', '!', "*"]
test_string = " ::= a | a "
print ("Original String : " + test_string)
delete_dict = {sp_character: '' for sp_character in string.punctuation}
delete_dict[' '] = ''
table = str.maketrans(delete_dict)
test_string = test_string.translate(table)
print ("Resultant list is : " + str
(test_string))
2.
The weakest precondition is
y = 2 x + 1
y = 3 x − 1 { y > 3}
3.
#include
int main()
{ int n = 1;
while(n != 1)
{ if(n % 2 == 0){ //for even numbers
n = n / 2;
printf("%d\n", n);
} else{ //for odd numbers
n = n * 3 + 1;
printf("%d\n", n);
} }
return 0;}
4.
#include
using namespace std;
int convert(int m, int n)
{
if (m == n)
return 0;
if (m > n)
return m - n;
if (m <= 0 && n > 0)
return -1;
if (n % 2 == 1)
return 1 + convert(m, n + 1);
else
return 1 + convert(m, n / 2);
}
int main()
{
int m = 3, n = 11;
cout << "Minimum number of operations : "
<< convert(m, n);
return 0;
}
5.
#include
#include
#include
#include
#include
using namespace std;
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();
for(i = 0; i < 6; ++i){
if(ch == operators[i])
cout< }
if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
cout< else
cout< }
}
fin.close();
return 0;
}
6.
#include
#include
#include
#include
// Returns 'true' if the character is a DELIMITER.
bool isDelimiter(char ch)
{
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}
// Returns 'true' if the character is an OPERATOR.
bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}
// Returns 'true' if the string is a VALID IDENTIFIER.
bool validIdentifier(char* str)
{
if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isDelimiter(str[0]) == true)
return (false);
return (true);
}
// Returns 'true' if the string is a KEYWORD.
bool isKeyword(char* str)
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "while") || !strcmp(str, "do") ||
!strcmp(str, "break") ||
!strcmp(str, "continue") || !strcmp(str, "int")
|| !strcmp(str, "double") || !strcmp(str, "float")
||...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here