Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

given: emcee.l %option noyywrap D [0-9] A [a-zA-Z] %{ #include tokens.h char val[100]; %} %% def return (DEF); integer return (INTEGER); real return (REAL); string

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

given: emcee.l

%option noyywrap

D [0-9]

A [a-zA-Z]

%{

#include "tokens.h"

char val[100];

%}

%%

def return (DEF);

integer return (INTEGER);

real return (REAL);

string return (STRING);

if return (IF);

then return (THEN);

while return (WHILE);

{D}+"."{D}+ { sprintf(val, "Real:%s", yytext); return (REAL_CONST); }

{A}+ { strcpy(val, yytext); return (ID); }

{D}+ { sprintf(val, "Integer:%s", yytext); return (INT_CONST); }

\"[^"]*\" { sprintf(val, "String:%s", yytext); return (STRING_CONST); }

";" return (SEMI);

":" return (COLON);

"(" return (LEFT_PAREN);

")" return (RIGHT_PAREN);

"+" return (PLUS);

"-" return (MINUS);

"*" return (MULT);

"/" return (DIVIDE);

"^" return (CARAT);

"%" return (MOD);

"=" return (EQUAL);

"!=" return (NOT_EQUAL);

"

">" return (GREATER_THAN);

"

">=" return (GREATER_EQUAL);

":=" return (ASSIGN);

"[" return (LEFT_SQUARE);

"]" return (RIGHT_SQUARE);

"{" return (LEFT_BRACE);

"}" return (RIGHT_BRACE);

"," return (COMMA);

^#.* ; /* Ignore comments */

. ; /* ignore any unmatched chars */

" " ;

%%

void yyerror () /* default action in case of error in yylex() */

{

printf (" error ");

exit(0);

}

given: emcee.y

%{

#include

#include

#include

int yylex (void);

void yyerror (char const *);

extern char val[100];

%}

%define api.value.type {char *}

%token DEF 1

%token INTEGER 2

%token REAL 3

%token STRING 4

%token IF 5

%token THEN 6

%token WHILE 7

%token REAL_CONST 8

%token ID 9

%token INT_CONST 10

%token STRING_CONST 11

%token SEMI 12

%token COLON 13

%token LEFT_PAREN 14

%token RIGHT_PAREN 15

%token PLUS 16

%token MINUS 17

%token MULT 18

%token DIVIDE 19

%token CARAT 20

%token MOD 21

%token EQUAL 22

%token NOT_EQUAL 23

%token LESS_THAN 24

%token GREATER_THAN 25

%token LESS_EQUAL 26

%token GREATER_EQUAL 27

%token ASSIGN 28

%token LEFT_SQUARE 29

%token RIGHT_SQUARE 30

%token LEFT_BRACE 31

%token RIGHT_BRACE 32

%token COMMA 33

%% /* Grammar rules and actions follow. */

input : statement_list

;

statement_list

:

| statement statement_list

;

statement

: variable_definition

| assignment_statement

| conditional_statement

| loop_statement

| block_statement

| expression SEMI

;

variable_definition

: DEF identifier COLON type SEMI

{ printf("Variable %s defined as a %s ", $2, $4); }

;

identifier

: ID

{ $$ = strdup(val); }

;

type : INTEGER

{ $$ = "integer"; }

| REAL

{ $$ = "real"; }

| STRING

{ $$ = "string"; }

;

assignment_statement

: identifier ASSIGN expression SEMI

{ printf("Assigning %s to an expression ", $1); }

;

constant

: REAL_CONST

{ $$ = strdup(val); }

| INT_CONST

{ $$ = strdup(val); }

| STRING_CONST

{ $$ = strdup(val); }

;

operator

: PLUS

{ $$ = "addition"; }

;

given: emceeparse.c

#include

int yyparse();

main () {

printf("Emcee Compiler ver. 0.2 ");

yyparse();

}

given: tokens.h

#define DEF 1

#define INTEGER 2

#define REAL 3

#define STRING 4

#define IF 5

#define THEN 6

#define WHILE 7

#define REAL_CONST 8

#define ID 9

#define INT_CONST 10

#define STRING_CONST 11

#define SEMI 12

#define COLON 13

#define LEFT_PAREN 14

#define RIGHT_PAREN 15

#define PLUS 16

#define MINUS 17

#define MULT 18

#define DIVIDE 19

#define CARAT 20

#define MOD 21

#define EQUAL 22

#define NOT_EQUAL 23

#define LESS_THAN 24

#define GREATER_THAN 25

#define LESS_EQUAL 26

#define GREATER_EQUAL 27

#define ASSIGN 28

#define LEFT_SQUARE 29

#define RIGHT_SQUARE 30

#define LEFT_BRACE 31

#define RIGHT_BRACE 32

#define COMMA 33

given: sqrt.mc

# First program written in Emcee

def guess : real; def n : real;

# Set the input variables guess := 1.0; n := inputReal();

if [ n >= 0.0 ] then {

# Continue until the root has been found

while [ abs(guess^2 - n) > 0.01 ] { guess := (n + (n / guess)) / 2; };

# Print the result print("Root is ", guess); };

1. Problem Description: At EMCC, we continue building our own computer language called Emcee. As we have learned in class, there are four different levels of analysis . Lexical-identifiers, keywords, punctuation, comments, etc. Syntactic-the structures within the language (e.g. definitions, if-statements) Contextual-static checking of variables, type, name, initialization, etc. . . . Semantic-meaning and behavior of a program. You completed the first step in the last assignment (Lexical Analysis). It is responsible for reading the input source code file and breaking it up into the various tokens defined by the language The next step is to handle the second level, Syntactic, by creating a Parser that can recognize the language structures found in the source text. The parser is only concern with these structures. It uses the Scanner to hand it the tokens that were found in the source You may want to review the description of the Emcee language again ("Emcee Language Overview.doc") as well as your initial cut at the BNF definitions you wrote for the language. You will need a definition of all these language structures to create the parser. Notice that the emcee language does not have a 'function'. It is just a list of statements. This is where we wil start. Writing a parser is even harder than writing a scanner. However, programmers have been building them since the dawn of high level languages. Because of that, tools have been developed that wil generate the parser for you. All you need to do is describe the language structures (in BNF) that the language expects to find and the tool will create the parser code. Neat. The tool you have installed on your machine is called bison. It is a descendent of the original UNIX parser generator called Yacc (Yet Another Compiler Compiler). Bison, Yacc, get it? If you didn't instal it when you built your machine, do this first: sudo apt-get -y install bison Bison takes the description of the language that you developed for the BNF assignment. You can learn much more about using Bison here: https://www.gnu.org/software/bison/ To assist in this job, I have provided a starting file for a description of the Emcee language structures. This can be found in emcee.y (Yacc/Bison source files are typically stored in .y files) The scanner and the parser will work together. In order for this to happen, the two functions must agree on a couple things 1. Problem Description: At EMCC, we continue building our own computer language called Emcee. As we have learned in class, there are four different levels of analysis . Lexical-identifiers, keywords, punctuation, comments, etc. Syntactic-the structures within the language (e.g. definitions, if-statements) Contextual-static checking of variables, type, name, initialization, etc. . . . Semantic-meaning and behavior of a program. You completed the first step in the last assignment (Lexical Analysis). It is responsible for reading the input source code file and breaking it up into the various tokens defined by the language The next step is to handle the second level, Syntactic, by creating a Parser that can recognize the language structures found in the source text. The parser is only concern with these structures. It uses the Scanner to hand it the tokens that were found in the source You may want to review the description of the Emcee language again ("Emcee Language Overview.doc") as well as your initial cut at the BNF definitions you wrote for the language. You will need a definition of all these language structures to create the parser. Notice that the emcee language does not have a 'function'. It is just a list of statements. This is where we wil start. Writing a parser is even harder than writing a scanner. However, programmers have been building them since the dawn of high level languages. Because of that, tools have been developed that wil generate the parser for you. All you need to do is describe the language structures (in BNF) that the language expects to find and the tool will create the parser code. Neat. The tool you have installed on your machine is called bison. It is a descendent of the original UNIX parser generator called Yacc (Yet Another Compiler Compiler). Bison, Yacc, get it? If you didn't instal it when you built your machine, do this first: sudo apt-get -y install bison Bison takes the description of the language that you developed for the BNF assignment. You can learn much more about using Bison here: https://www.gnu.org/software/bison/ To assist in this job, I have provided a starting file for a description of the Emcee language structures. This can be found in emcee.y (Yacc/Bison source files are typically stored in .y files) The scanner and the parser will work together. In order for this to happen, the two functions must agree on a couple things

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

List the different categories of international employees. page 642

Answered: 1 week ago

Question

Explain the legal environments impact on labor relations. page 590

Answered: 1 week ago