-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.txt
109 lines (73 loc) · 3.37 KB
/
Parser.txt
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
What is a Parser?
A parser is a software component that takes input data (usually in the form of a text file or a string) and builds a data structure, usually in the form of a tree or a graph, that represents the structure of the input data. Parsers are used in a wide range of applications, including compilers, interpreters, text editors, and web browsers.
Types of Parsers
There are several types of parsers, each with its own set of features and capabilities:
Top-down parsers: These parsers start at the root of the tree and build the tree down towards the leaves. They are usually easier to implement, but they may not be as efficient as bottom-up parsers.
Bottom-up parsers: These parsers start at the leaves of the tree and build the tree up towards the root. They are usually more efficient than top-down parsers, but they may be more difficult to implement.
LL parsers: These parsers are top-down parsers that build the tree from left to right, using a leftmost derivation. They are usually efficient and easy to implement, but they may not be able to handle all types of grammars.
LR parsers: These parsers are bottom-up parsers that build the tree from left to right, using a rightmost derivation. They are usually more powerful than LL parsers, but they may be more difficult to implement.
Python Parser Libraries
There are several parser libraries available for Python, including:
PLY: A simple, easy-to-use library for creating lexers and parsers.
ANTLR: A powerful parser generator that supports a wide range of programming languages.
pyparsing: A library for creating parsers using a simple, declarative syntax.
Example
Here is an example of how you can use the PLY library to create a simple calculator parser in Python:
import ply.lex as lex
import ply.yacc as yacc
# Define the lexical tokens
tokens = ['NUMBER', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE']
# Define the lexer rules
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# Ignore whitespace
t_ignore = ' \t'
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Define the parser rules
def p_expression_plus(p):
'expression : expression PLUS term'
p[0] = p[1] + p[3]
def p_expression_minus(p):
'expression : expression MINUS term'
p[0] = p[1] - p[3]
def p_expression_term(p):
'expression : term'
p[0] = p[1]
def p_term_times(p):
'term : term TIMES factor'
p[0] = p[1] * p[3]
def p_term_div(p):
'term : term DIVIDE factor'
p[0] = p[1] / p[3]
def p_term_factor(p):
'term : factor'
p[0] = p[1]
def p_factor_num(p):
'factor : NUMBER'
p[0] = p[1]
def p_factor_expr(p):
'factor : LPAREN expression RPAREN'
p[0] = p[2]
def p_error(p):
print("Syntax error in input!")
import ply.yacc as yacc
parser = yacc.yacc()
while True:
try:
s = input('calc > ')
except EOFError:
break
if not s: continue
result = parser.parse(s)
print(result)
This code defines a simple calculator parser that can parse expressions containing numbers and the basic arithmetic operators. It uses the PLY library to define the lexical tokens, the lexer rules, and the parser rules. The lexer converts the input string into a stream of tokens, and the parser uses these tokens to build a parse tree.