write LEX file
This commit is contained in:
@@ -1,5 +1,24 @@
|
|||||||
Created by PLY version 3.11 (http://www.dabeaz.com/ply)
|
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
|
Grammar
|
||||||
|
|
||||||
Rule 0 S' -> expression
|
Rule 0 S' -> expression
|
||||||
@@ -8,10 +27,26 @@ Rule 2 expression -> WHILE expression DO LBRACE expression RBRACE
|
|||||||
|
|
||||||
Terminals, with rules where they appear
|
Terminals, with rules where they appear
|
||||||
|
|
||||||
|
AOP :
|
||||||
|
ASSIGN :
|
||||||
|
COMMA :
|
||||||
CONST : 1
|
CONST : 1
|
||||||
DO : 2
|
DO : 2
|
||||||
|
ELSE :
|
||||||
|
FALSE :
|
||||||
|
ID :
|
||||||
|
IF :
|
||||||
|
IN :
|
||||||
LBRACE : 2
|
LBRACE : 2
|
||||||
|
LET :
|
||||||
|
LOP :
|
||||||
|
LPAREN :
|
||||||
RBRACE : 2
|
RBRACE : 2
|
||||||
|
RELOP :
|
||||||
|
RPAREN :
|
||||||
|
SEMICOLON :
|
||||||
|
THEN :
|
||||||
|
TRUE :
|
||||||
WHILE : 2
|
WHILE : 2
|
||||||
error :
|
error :
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ _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,]),}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user