Question: Add the following semantics ( context - sensitive ) specifications to the parsing code in Arithmetic operations are performed such that a - b =
Add the following semantics contextsensitive specifications to the parsing
code in
Arithmetic operations are performed such that max
division by zero gives the result and the remainder also
mean addition, subtraction, multiplication, integer division, and
calculating the remainder.
: means assignment.
The declaration tab: declares a tab array with elements indexed
from to The identifier tabi refers to the ith element of the tab
array. A declaration containing the first number greater than the second
should be reported as an error.
The FOR loop has a local iterator, which takes values from the value after
FROM to the value after TO in an interval of or if the word
DOWNTO is used.
The number of iterations of the FOR loop is set at the beginning and does
not change during its execution even if the values of the variables
determining the beginning and end of the loop change
The FOR loop iterator cannot be modified inside the loop the compiler
should report an error in such a case
The REPEATUNTIL loop ends when the condition written after UNTIL is
met the loop will execute at least once
The READ statement reads the value from outside and substitutes it into
the variable and WRITE writes the value of the variablenumber outside.
The remaining instructions are consistent with their meaning in most
programming languages.
import plyyacc as yacc
import plylex as lex
# Code for lexical analysis:
tokens # List with TOKENS names
'KEYWORD',
ID
'NUM',
LBRRBR
'COLON', 'SEMICOLON', 'COMMA',
'ASSIGN',
EQ 'NEQ', 'LEQ', 'GEQ',
LTGT
'PLUS', 'MINUS', 'MULT', 'DIV', 'MOD'
reserved DECLARE: 'DECLARE', # Created dictionary with KEYWORDS, with a rule that matches with the identifier
'BEGIN': 'BEGIN',
'END': 'END',
IF: IF
'THEN': 'THEN',
'ELSE': 'ELSE',
'ENDIF': 'ENDIF',
DO: DO
'FOR': 'FOR',
'FROM': 'FROM',
TO: TO
'DOWNTO': 'DOWNTO',
'ENDFOR': 'ENDFOR',
'WHILE': 'WHILE',
'REPEAT': 'REPEAT',
'UNTIL': 'UNTIL',
'ENDWHILE': 'ENDWHILE',
'READ': 'READ',
'WRITE': 'WRITE'
# Regular expression rules for tokens
tLBR r
tRBR r
tCOLON r:
tSEMICOLON r;
tCOMMA r
tASSIGN r:
tEQ r
tNEQ r
tLEQ r
tGEQ r
tLT r
tGT r
tPLUS r
tMINUS r
tMULT r
tDIV r
tMOD
tignore t # Lexer ignores spaces
tokens tokens listreservedvalues # Keywords are defined as tokens too
def tKEYWORDt: # Defined a rule to track KEYWORDS.
rAZAZ # A string with one or more uppercase letters
ttype reserved.gettvalue, 'KEYWORD'
return t
def tIDt: # Defined rule to track IDs
razaz # String with one or more consecutive lowercase letters
ttype ID
return t
def tNUMt: # Defined to identify numbers
rd # A digit or a repetition of digits
tvalue inttvalue
return t
def tCOMMENTt: # Defined so lexer ignores comments
r$
pass
def tnewlinet: # Tracks the number of lines
r
tlexer.lineno lentvalue
def terrort: # Defined rule to identify errors
printIllegal character s tvalue
tlexer.skip
lexer lex.lex # Created lexer
printName of file for lexical analysis and parsing:"
textName input # Input of the name of the file, as one should try different files
textFile opentextNamer # Opens text file
data textFile.read # Data that is going to be used on the lexer
lexer.inputdata # Entered the data in the lexer
# While loop to tokenizeIdentify token, value, row and length
while True:
toke lexer.token
if not toke:
break
printtoke
parsingsuccessful True # Flag to indicate parsing success, so it only prints when parsing is successful
def pprogramp: # Define instructions for prgram
program : DECLARE declarations BEGIN commands END
BEGIN commands END"""
def pdeclarationsp: # Define instructions for declarations
declarations : declarations COMMA ID
declarations COMMA ID LBR NUM COLON NUM RBR
ID
ID LBR NUM COLON NUM RBR
def pcommandsp: # Define instructions for commands
commands : commands command
command
def pcommandp: # Define instructions for command, that are used in commands
command : identifier ASSIGN ex
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
