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,5 +1,24 @@
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
@@ -8,10 +27,26 @@ 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 :

View File

@@ -6,7 +6,7 @@ _tabversion = '3.10'
_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,]),}

View File

@@ -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)