write LEX file
This commit is contained in:
@@ -1,100 +1,135 @@
|
||||
Created by PLY version 3.11 (http://www.dabeaz.com/ply)
|
||||
|
||||
Grammar
|
||||
|
||||
Rule 0 S' -> expression
|
||||
Rule 1 expression -> CONST
|
||||
Rule 2 expression -> WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
Terminals, with rules where they appear
|
||||
|
||||
CONST : 1
|
||||
DO : 2
|
||||
LBRACE : 2
|
||||
RBRACE : 2
|
||||
WHILE : 2
|
||||
error :
|
||||
|
||||
Nonterminals, with rules where they appear
|
||||
|
||||
expression : 2 2 0
|
||||
|
||||
Parsing method: LALR
|
||||
|
||||
state 0
|
||||
|
||||
(0) S' -> . expression
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 1
|
||||
|
||||
state 1
|
||||
|
||||
(0) S' -> expression .
|
||||
|
||||
|
||||
|
||||
state 2
|
||||
|
||||
(1) expression -> CONST .
|
||||
|
||||
$end reduce using rule 1 (expression -> CONST .)
|
||||
DO reduce using rule 1 (expression -> CONST .)
|
||||
RBRACE reduce using rule 1 (expression -> CONST .)
|
||||
|
||||
|
||||
state 3
|
||||
|
||||
(2) expression -> WHILE . expression DO LBRACE expression RBRACE
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 4
|
||||
|
||||
state 4
|
||||
|
||||
(2) expression -> WHILE expression . DO LBRACE expression RBRACE
|
||||
|
||||
DO shift and go to state 5
|
||||
|
||||
|
||||
state 5
|
||||
|
||||
(2) expression -> WHILE expression DO . LBRACE expression RBRACE
|
||||
|
||||
LBRACE shift and go to state 6
|
||||
|
||||
|
||||
state 6
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE . expression RBRACE
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 7
|
||||
|
||||
state 7
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE expression . RBRACE
|
||||
|
||||
RBRACE shift and go to state 8
|
||||
|
||||
|
||||
state 8
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE expression RBRACE .
|
||||
|
||||
$end reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
DO reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
RBRACE reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
|
||||
Created by PLY version 3.11 (http://www.dabeaz.com/ply)
|
||||
|
||||
Unused terminals:
|
||||
|
||||
AOP
|
||||
ASSIGN
|
||||
COMMA
|
||||
ELSE
|
||||
FALSE
|
||||
ID
|
||||
IF
|
||||
IN
|
||||
LET
|
||||
LOP
|
||||
LPAREN
|
||||
RELOP
|
||||
RPAREN
|
||||
SEMICOLON
|
||||
THEN
|
||||
TRUE
|
||||
|
||||
Grammar
|
||||
|
||||
Rule 0 S' -> expression
|
||||
Rule 1 expression -> CONST
|
||||
Rule 2 expression -> WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
Terminals, with rules where they appear
|
||||
|
||||
AOP :
|
||||
ASSIGN :
|
||||
COMMA :
|
||||
CONST : 1
|
||||
DO : 2
|
||||
ELSE :
|
||||
FALSE :
|
||||
ID :
|
||||
IF :
|
||||
IN :
|
||||
LBRACE : 2
|
||||
LET :
|
||||
LOP :
|
||||
LPAREN :
|
||||
RBRACE : 2
|
||||
RELOP :
|
||||
RPAREN :
|
||||
SEMICOLON :
|
||||
THEN :
|
||||
TRUE :
|
||||
WHILE : 2
|
||||
error :
|
||||
|
||||
Nonterminals, with rules where they appear
|
||||
|
||||
expression : 2 2 0
|
||||
|
||||
Parsing method: LALR
|
||||
|
||||
state 0
|
||||
|
||||
(0) S' -> . expression
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 1
|
||||
|
||||
state 1
|
||||
|
||||
(0) S' -> expression .
|
||||
|
||||
|
||||
|
||||
state 2
|
||||
|
||||
(1) expression -> CONST .
|
||||
|
||||
$end reduce using rule 1 (expression -> CONST .)
|
||||
DO reduce using rule 1 (expression -> CONST .)
|
||||
RBRACE reduce using rule 1 (expression -> CONST .)
|
||||
|
||||
|
||||
state 3
|
||||
|
||||
(2) expression -> WHILE . expression DO LBRACE expression RBRACE
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 4
|
||||
|
||||
state 4
|
||||
|
||||
(2) expression -> WHILE expression . DO LBRACE expression RBRACE
|
||||
|
||||
DO shift and go to state 5
|
||||
|
||||
|
||||
state 5
|
||||
|
||||
(2) expression -> WHILE expression DO . LBRACE expression RBRACE
|
||||
|
||||
LBRACE shift and go to state 6
|
||||
|
||||
|
||||
state 6
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE . expression RBRACE
|
||||
(1) expression -> . CONST
|
||||
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
|
||||
|
||||
CONST shift and go to state 2
|
||||
WHILE shift and go to state 3
|
||||
|
||||
expression shift and go to state 7
|
||||
|
||||
state 7
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE expression . RBRACE
|
||||
|
||||
RBRACE shift and go to state 8
|
||||
|
||||
|
||||
state 8
|
||||
|
||||
(2) expression -> WHILE expression DO LBRACE expression RBRACE .
|
||||
|
||||
$end reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
DO reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
RBRACE reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
|
||||
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
|
||||
# parsetab.py
|
||||
# This file is automatically generated. Do not edit.
|
||||
# pylint: disable=W,C,R
|
||||
_tabversion = '3.10'
|
||||
|
||||
_lr_method = 'LALR'
|
||||
|
||||
_lr_signature = 'CONST DO LBRACE RBRACE WHILEexpression : CONSTexpression : WHILE expression DO LBRACE expression RBRACE'
|
||||
|
||||
_lr_action_items = {'CONST':([0,3,6,],[2,2,2,]),'WHILE':([0,3,6,],[3,3,3,]),'$end':([1,2,8,],[0,-1,-2,]),'DO':([2,4,8,],[-1,5,-2,]),'RBRACE':([2,7,8,],[-1,8,-2,]),'LBRACE':([5,],[6,]),}
|
||||
|
||||
_lr_action = {}
|
||||
for _k, _v in _lr_action_items.items():
|
||||
for _x,_y in zip(_v[0],_v[1]):
|
||||
if not _x in _lr_action: _lr_action[_x] = {}
|
||||
_lr_action[_x][_k] = _y
|
||||
del _lr_action_items
|
||||
|
||||
_lr_goto_items = {'expression':([0,3,6,],[1,4,7,]),}
|
||||
|
||||
_lr_goto = {}
|
||||
for _k, _v in _lr_goto_items.items():
|
||||
for _x, _y in zip(_v[0], _v[1]):
|
||||
if not _x in _lr_goto: _lr_goto[_x] = {}
|
||||
_lr_goto[_x][_k] = _y
|
||||
del _lr_goto_items
|
||||
_lr_productions = [
|
||||
("S' -> expression","S'",1,None,None,None),
|
||||
('expression -> CONST','expression',1,'p_expression_const','triplayacc.py',28),
|
||||
('expression -> WHILE expression DO LBRACE expression RBRACE','expression',6,'p_expression_while','triplayacc.py',33),
|
||||
]
|
||||
|
||||
# parsetab.py
|
||||
# This file is automatically generated. Do not edit.
|
||||
# pylint: disable=W,C,R
|
||||
_tabversion = '3.10'
|
||||
|
||||
_lr_method = 'LALR'
|
||||
|
||||
_lr_signature = 'AOP ASSIGN COMMA CONST DO ELSE FALSE ID IF IN LBRACE LET LOP LPAREN RBRACE RELOP RPAREN SEMICOLON THEN TRUE WHILEexpression : CONSTexpression : WHILE expression DO LBRACE expression RBRACE'
|
||||
|
||||
_lr_action_items = {'CONST':([0,3,6,],[2,2,2,]),'WHILE':([0,3,6,],[3,3,3,]),'$end':([1,2,8,],[0,-1,-2,]),'DO':([2,4,8,],[-1,5,-2,]),'RBRACE':([2,7,8,],[-1,8,-2,]),'LBRACE':([5,],[6,]),}
|
||||
|
||||
_lr_action = {}
|
||||
for _k, _v in _lr_action_items.items():
|
||||
for _x,_y in zip(_v[0],_v[1]):
|
||||
if not _x in _lr_action: _lr_action[_x] = {}
|
||||
_lr_action[_x][_k] = _y
|
||||
del _lr_action_items
|
||||
|
||||
_lr_goto_items = {'expression':([0,3,6,],[1,4,7,]),}
|
||||
|
||||
_lr_goto = {}
|
||||
for _k, _v in _lr_goto_items.items():
|
||||
for _x, _y in zip(_v[0], _v[1]):
|
||||
if not _x in _lr_goto: _lr_goto[_x] = {}
|
||||
_lr_goto[_x][_k] = _y
|
||||
del _lr_goto_items
|
||||
_lr_productions = [
|
||||
("S' -> expression","S'",1,None,None,None),
|
||||
('expression -> CONST','expression',1,'p_expression_const','triplayacc.py',28),
|
||||
('expression -> WHILE expression DO LBRACE expression RBRACE','expression',6,'p_expression_while','triplayacc.py',33),
|
||||
]
|
||||
|
||||
@@ -1,43 +1,79 @@
|
||||
# ------------------------------------------------------------
|
||||
# triplalex.py
|
||||
#
|
||||
# tokenizer for the TRIPLA parser
|
||||
# Tokenizer for the TRIPLA parser
|
||||
# ------------------------------------------------------------
|
||||
|
||||
import ply.lex as lex
|
||||
|
||||
|
||||
reserved = {
|
||||
'while' : 'WHILE',
|
||||
'do' : 'DO',
|
||||
'let': 'LET',
|
||||
'in': 'IN',
|
||||
'if': 'IF',
|
||||
'then': 'THEN',
|
||||
'else': 'ELSE',
|
||||
'while': 'WHILE',
|
||||
'do': 'DO',
|
||||
'true': 'TRUE',
|
||||
'false': 'FALSE'
|
||||
}
|
||||
|
||||
# List of token names. This is always required
|
||||
tokens = [
|
||||
'ID',
|
||||
'CONST',
|
||||
'LBRACE', 'RBRACE'
|
||||
]+list(reserved.values())
|
||||
'AOP',
|
||||
'RELOP',
|
||||
'LOP',
|
||||
'ASSIGN',
|
||||
'LPAREN', 'RPAREN',
|
||||
'LBRACE', 'RBRACE',
|
||||
'COMMA',
|
||||
'SEMICOLON',
|
||||
] + list(reserved.values())
|
||||
|
||||
# Regular expression rules for simple tokens
|
||||
t_LBRACE = r'\{'
|
||||
t_RBRACE = r'\}'
|
||||
t_WHILE = r'while'
|
||||
t_DO = r'do'
|
||||
# Simple tokens
|
||||
t_LPAREN = r'\('
|
||||
t_RPAREN = r'\)'
|
||||
t_LBRACE = r'\{'
|
||||
t_RBRACE = r'\}'
|
||||
t_COMMA = r','
|
||||
t_SEMICOLON = r';'
|
||||
t_ASSIGN = r'='
|
||||
|
||||
# A regular expression rule with some action code
|
||||
# Arithmetic operators
|
||||
t_AOP = r'\+|\-|\*|/'
|
||||
|
||||
# Comparison operators
|
||||
t_RELOP = r'<=|>=|==|!=|<|>'
|
||||
|
||||
# Logical operators
|
||||
t_LOP = r'\|\||&&|==|!='
|
||||
|
||||
# IDs
|
||||
def t_ID(t):
|
||||
r'[A-Za-z_][A-Za-z0-9_]*'
|
||||
t.type = reserved.get(t.value, 'ID')
|
||||
return t
|
||||
|
||||
# Constants
|
||||
def t_CONST(t):
|
||||
r'\d+'
|
||||
r'0|[1-9][0-9]*'
|
||||
t.value = int(t.value)
|
||||
return t
|
||||
|
||||
# Define a rule so we can track line numbers
|
||||
# Linebreaks
|
||||
def t_newline(t):
|
||||
r'\n+'
|
||||
t.lexer.lineno += len(t.value)
|
||||
|
||||
# A string containing ignored characters (spaces and tabs)
|
||||
# Ignore whitespace
|
||||
t_ignore = ' \t'
|
||||
|
||||
# Error handling rule
|
||||
# Comments
|
||||
def t_comment(t):
|
||||
r'//.*'
|
||||
pass
|
||||
|
||||
# Error handling
|
||||
def t_error(t):
|
||||
print("Illegal character '%s'" % t.value[0])
|
||||
t.lexer.skip(1)
|
||||
|
||||
Reference in New Issue
Block a user