-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
75 lines (58 loc) · 1.73 KB
/
parser.py
File metadata and controls
75 lines (58 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import ply.yacc as yacc
from lexer import tokens
syntax_error = False
def p_program(p):
'''program : statement
| statement program'''
pass
def p_statements(p):
'''statements : statement
| statement statements'''
pass
def p_statement_if_else(p):
'''statement : IF LBRACKET condition RBRACKET THEN statements FI
| IF LBRACKET condition RBRACKET THEN statements ELSE statements FI'''
pass
def p_statement_for(p):
'''statement : FOR VARIABLE IN condition DO statements DONE'''
pass
def p_statement_while(p):
'''statement : WHILE LBRACKET condition RBRACKET DO statements DONE'''
pass
def p_statement_function_def(p):
'''statement : FUNCTION VARIABLE LPAREN RPAREN LFBRACKET statements RFBRACKET'''
pass
def p_statement_assign(p):
'''statement : VARIABLE EQUAL expression'''
pass
def p_statement_echo(p):
'''statement : ECHO QUOTES statements QUOTES
| ECHO DOLLAR VARIABLE'''
pass
def p_condition(p):
'''condition : expression
| expression operator expression'''
pass
def p_expression(p):
'''expression : VARIABLE
| NUMBER
| expression operator expression'''
pass
def p_operator(p):
'''operator : PLUS
| MINUS
| MUL
| DIV
| GT
| LT
| EQUAL'''
pass
def p_error(p):
global syntax_error
syntax_error = True
if p:
print(f"Syntax error at '{p.value}' (line {p.lineno}, position {p.lexpos})")
else:
print("Syntax error at EOF")
# Build parser
parser = yacc.yacc()