write LEX file

This commit is contained in:
Jan-Niclas Loosen
2025-11-19 00:50:46 +01:00
parent fe33668b5f
commit 622ecef369
3 changed files with 221 additions and 150 deletions

View File

@@ -1,100 +1,135 @@
Created by PLY version 3.11 (http://www.dabeaz.com/ply) Created by PLY version 3.11 (http://www.dabeaz.com/ply)
Grammar Unused terminals:
Rule 0 S' -> expression AOP
Rule 1 expression -> CONST ASSIGN
Rule 2 expression -> WHILE expression DO LBRACE expression RBRACE COMMA
ELSE
Terminals, with rules where they appear FALSE
ID
CONST : 1 IF
DO : 2 IN
LBRACE : 2 LET
RBRACE : 2 LOP
WHILE : 2 LPAREN
error : RELOP
RPAREN
Nonterminals, with rules where they appear SEMICOLON
THEN
expression : 2 2 0 TRUE
Parsing method: LALR Grammar
state 0 Rule 0 S' -> expression
Rule 1 expression -> CONST
(0) S' -> . expression Rule 2 expression -> WHILE expression DO LBRACE expression RBRACE
(1) expression -> . CONST
(2) expression -> . WHILE expression DO LBRACE expression RBRACE Terminals, with rules where they appear
CONST shift and go to state 2 AOP :
WHILE shift and go to state 3 ASSIGN :
COMMA :
expression shift and go to state 1 CONST : 1
DO : 2
state 1 ELSE :
FALSE :
(0) S' -> expression . ID :
IF :
IN :
LBRACE : 2
state 2 LET :
LOP :
(1) expression -> CONST . LPAREN :
RBRACE : 2
$end reduce using rule 1 (expression -> CONST .) RELOP :
DO reduce using rule 1 (expression -> CONST .) RPAREN :
RBRACE reduce using rule 1 (expression -> CONST .) SEMICOLON :
THEN :
TRUE :
state 3 WHILE : 2
error :
(2) expression -> WHILE . expression DO LBRACE expression RBRACE
(1) expression -> . CONST Nonterminals, with rules where they appear
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
expression : 2 2 0
CONST shift and go to state 2
WHILE shift and go to state 3 Parsing method: LALR
expression shift and go to state 4 state 0
state 4 (0) S' -> . expression
(1) expression -> . CONST
(2) expression -> WHILE expression . DO LBRACE expression RBRACE (2) expression -> . WHILE expression DO LBRACE expression RBRACE
DO shift and go to state 5 CONST shift and go to state 2
WHILE shift and go to state 3
state 5 expression shift and go to state 1
(2) expression -> WHILE expression DO . LBRACE expression RBRACE state 1
LBRACE shift and go to state 6 (0) S' -> expression .
state 6
state 2
(2) expression -> WHILE expression DO LBRACE . expression RBRACE
(1) expression -> . CONST (1) expression -> CONST .
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
$end reduce using rule 1 (expression -> CONST .)
CONST shift and go to state 2 DO reduce using rule 1 (expression -> CONST .)
WHILE shift and go to state 3 RBRACE reduce using rule 1 (expression -> CONST .)
expression shift and go to state 7
state 3
state 7
(2) expression -> WHILE . expression DO LBRACE expression RBRACE
(2) expression -> WHILE expression DO LBRACE expression . RBRACE (1) expression -> . CONST
(2) expression -> . WHILE expression DO LBRACE expression RBRACE
RBRACE shift and go to state 8
CONST shift and go to state 2
WHILE shift and go to state 3
state 8
expression shift and go to state 4
(2) expression -> WHILE expression DO LBRACE expression RBRACE .
state 4
$end reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .)
DO reduce using rule 2 (expression -> WHILE expression DO LBRACE expression RBRACE .) (2) expression -> WHILE expression . DO LBRACE expression RBRACE
RBRACE reduce using rule 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 .)

View File

@@ -1,32 +1,32 @@
# parsetab.py # parsetab.py
# This file is automatically generated. Do not edit. # This file is automatically generated. Do not edit.
# pylint: disable=W,C,R # pylint: disable=W,C,R
_tabversion = '3.10' _tabversion = '3.10'
_lr_method = 'LALR' _lr_method = 'LALR'
_lr_signature = 'CONST DO LBRACE RBRACE WHILEexpression : CONSTexpression : WHILE expression DO LBRACE expression RBRACE' _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_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 = {} _lr_action = {}
for _k, _v in _lr_action_items.items(): for _k, _v in _lr_action_items.items():
for _x,_y in zip(_v[0],_v[1]): for _x,_y in zip(_v[0],_v[1]):
if not _x in _lr_action: _lr_action[_x] = {} if not _x in _lr_action: _lr_action[_x] = {}
_lr_action[_x][_k] = _y _lr_action[_x][_k] = _y
del _lr_action_items del _lr_action_items
_lr_goto_items = {'expression':([0,3,6,],[1,4,7,]),} _lr_goto_items = {'expression':([0,3,6,],[1,4,7,]),}
_lr_goto = {} _lr_goto = {}
for _k, _v in _lr_goto_items.items(): for _k, _v in _lr_goto_items.items():
for _x, _y in zip(_v[0], _v[1]): for _x, _y in zip(_v[0], _v[1]):
if not _x in _lr_goto: _lr_goto[_x] = {} if not _x in _lr_goto: _lr_goto[_x] = {}
_lr_goto[_x][_k] = _y _lr_goto[_x][_k] = _y
del _lr_goto_items del _lr_goto_items
_lr_productions = [ _lr_productions = [
("S' -> expression","S'",1,None,None,None), ("S' -> expression","S'",1,None,None,None),
('expression -> CONST','expression',1,'p_expression_const','triplayacc.py',28), ('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), ('expression -> WHILE expression DO LBRACE expression RBRACE','expression',6,'p_expression_while','triplayacc.py',33),
] ]

View File

@@ -1,43 +1,79 @@
# ------------------------------------------------------------ # ------------------------------------------------------------
# triplalex.py # Tokenizer for the TRIPLA parser
#
# tokenizer for the TRIPLA parser
# ------------------------------------------------------------ # ------------------------------------------------------------
import ply.lex as lex import ply.lex as lex
reserved = { reserved = {
'while' : 'WHILE', 'let': 'LET',
'do' : 'DO', 'in': 'IN',
'if': 'IF',
'then': 'THEN',
'else': 'ELSE',
'while': 'WHILE',
'do': 'DO',
'true': 'TRUE',
'false': 'FALSE'
} }
# List of token names. This is always required # List of token names. This is always required
tokens = [ tokens = [
'ID',
'CONST', 'CONST',
'LBRACE', 'RBRACE' 'AOP',
]+list(reserved.values()) 'RELOP',
'LOP',
'ASSIGN',
'LPAREN', 'RPAREN',
'LBRACE', 'RBRACE',
'COMMA',
'SEMICOLON',
] + list(reserved.values())
# Regular expression rules for simple tokens # Simple tokens
t_LBRACE = r'\{' t_LPAREN = r'\('
t_RBRACE = r'\}' t_RPAREN = r'\)'
t_WHILE = r'while' t_LBRACE = r'\{'
t_DO = r'do' 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): def t_CONST(t):
r'\d+' r'0|[1-9][0-9]*'
t.value = int(t.value) t.value = int(t.value)
return t return t
# Define a rule so we can track line numbers # Linebreaks
def t_newline(t): def t_newline(t):
r'\n+' r'\n+'
t.lexer.lineno += len(t.value) t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs) # Ignore whitespace
t_ignore = ' \t' t_ignore = ' \t'
# Error handling rule # Comments
def t_comment(t):
r'//.*'
pass
# Error handling
def t_error(t): def t_error(t):
print("Illegal character '%s'" % t.value[0]) print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1) t.lexer.skip(1)