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 (context-sensitive) specifications to the parsing
code in
Arithmetic operations are performed such that a-b=max{a-b,0},
division by zero gives the result 0 and the remainder also 0.
+-*/% mean addition, subtraction, multiplication, integer division, and
calculating the remainder.
:= means assignment.
The declaration tab(10:100) declares a tab array with 91 elements indexed
from 10 to 100. The identifier tab(i) refers to the i-th 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 +1 or -1 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 REPEAT-UNTIL 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 variable/number outside.
The remaining instructions are consistent with their meaning in most
programming languages.
import ply.yacc as yacc
import ply.lex as lex
# Code for lexical analysis:
tokens =[ # List with TOKENS names
'KEYWORD',
'ID',
'NUM',
'LBR','RBR',
'COLON', 'SEMICOLON', 'COMMA',
'ASSIGN',
'EQ', 'NEQ', 'LEQ', 'GEQ',
'LT','GT',
'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
t_LBR = r'\('
t_RBR = r'\)'
t_COLON = r':'
t_SEMICOLON = r';'
t_COMMA = r','
t_ASSIGN = r':='
t_EQ = r'='
t_NEQ = r'!='
t_LEQ = r'='
t_GEQ = r'>='
t_LT = r''
t_GT = r'>'
t_PLUS = r'\+'
t_MINUS = r'-'
t_MULT = r'\*'
t_DIV = r'/'
t_MOD ='%'
t_ignore ='\t' # Lexer ignores spaces
tokens = tokens + list(reserved.values()) # Keywords are defined as tokens too
def t_KEYWORD(t): # Defined a rule to track KEYWORDS.
r'[A-Z][A-Z]*' # A string with one or more uppercase letters
t.type = reserved.get(t.value, 'KEYWORD')
return t
def t_ID(t): # Defined rule to track IDs
r'[a-z][a-z_0-9]*' # String with one or more consecutive lowercase letters
t.type ='ID'
return t
def t_NUM(t): # Defined to identify numbers
r'\d+' # A digit or a repetition of digits
t.value = int(t.value)
return t
def t_COMMENT(t): # Defined so lexer ignores comments
r'\/\/(.*)$'
pass
def t_newline(t): # Tracks the number of lines
r'
+'
t.lexer.lineno += len(t.value)
def t_error(t): # Defined rule to identify errors
print("Illegal character '%s'"% t.value[0])
t.lexer.skip(1)
lexer = lex.lex() # Created lexer
print("Name of file for lexical analysis and parsing:")
textName = input() # Input of the name of the file, as one should try different files
textFile = open(textName,'r') # Opens text file
data = textFile.read() # Data that is going to be used on the lexer
lexer.input(data) # Entered the data in the lexer
# While loop to tokenize(Identify token, value, row and length
while True:
toke = lexer.token()
if not toke:
break
print(toke)
parsing_successful = True # Flag to indicate parsing success, so it only prints when parsing is successful
def p_program(p): # Define instructions for prgram
"""program : DECLARE declarations BEGIN commands END
| BEGIN commands END"""
def p_declarations(p): # Define instructions for declarations
""" declarations : declarations COMMA ID
| declarations COMMA ID LBR NUM COLON NUM RBR
| ID
| ID LBR NUM COLON NUM RBR"""
def p_commands(p): # Define instructions for commands
"""commands : commands command
| command """
def p_command(p): # Define instructions for command, that are used in commands
"""command : identifier ASSIGN ex
Add the following semantics ( context - sensitive

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!