Path: blob/main/third_party/ply/example/ansic/clex.py
7087 views
# ----------------------------------------------------------------------1# clex.py2#3# A lexer for ANSI C.4# ----------------------------------------------------------------------56import sys7sys.path.insert(0,"../..")89import ply.lex as lex1011# Reserved words12reserved = (13'AUTO', 'BREAK', 'CASE', 'CHAR', 'CONST', 'CONTINUE', 'DEFAULT', 'DO', 'DOUBLE',14'ELSE', 'ENUM', 'EXTERN', 'FLOAT', 'FOR', 'GOTO', 'IF', 'INT', 'LONG', 'REGISTER',15'RETURN', 'SHORT', 'SIGNED', 'SIZEOF', 'STATIC', 'STRUCT', 'SWITCH', 'TYPEDEF',16'UNION', 'UNSIGNED', 'VOID', 'VOLATILE', 'WHILE',17)1819tokens = reserved + (20# Literals (identifier, integer constant, float constant, string constant, char const)21'ID', 'TYPEID', 'ICONST', 'FCONST', 'SCONST', 'CCONST',2223# Operators (+,-,*,/,%,|,&,~,^,<<,>>, ||, &&, !, <, <=, >, >=, ==, !=)24'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'MOD',25'OR', 'AND', 'NOT', 'XOR', 'LSHIFT', 'RSHIFT',26'LOR', 'LAND', 'LNOT',27'LT', 'LE', 'GT', 'GE', 'EQ', 'NE',2829# Assignment (=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=)30'EQUALS', 'TIMESEQUAL', 'DIVEQUAL', 'MODEQUAL', 'PLUSEQUAL', 'MINUSEQUAL',31'LSHIFTEQUAL','RSHIFTEQUAL', 'ANDEQUAL', 'XOREQUAL', 'OREQUAL',3233# Increment/decrement (++,--)34'PLUSPLUS', 'MINUSMINUS',3536# Structure dereference (->)37'ARROW',3839# Conditional operator (?)40'CONDOP',4142# Delimeters ( ) [ ] { } , . ; :43'LPAREN', 'RPAREN',44'LBRACKET', 'RBRACKET',45'LBRACE', 'RBRACE',46'COMMA', 'PERIOD', 'SEMI', 'COLON',4748# Ellipsis (...)49'ELLIPSIS',50)5152# Completely ignored characters53t_ignore = ' \t\x0c'5455# Newlines56def t_NEWLINE(t):57r'\n+'58t.lexer.lineno += t.value.count("\n")5960# Operators61t_PLUS = r'\+'62t_MINUS = r'-'63t_TIMES = r'\*'64t_DIVIDE = r'/'65t_MOD = r'%'66t_OR = r'\|'67t_AND = r'&'68t_NOT = r'~'69t_XOR = r'\^'70t_LSHIFT = r'<<'71t_RSHIFT = r'>>'72t_LOR = r'\|\|'73t_LAND = r'&&'74t_LNOT = r'!'75t_LT = r'<'76t_GT = r'>'77t_LE = r'<='78t_GE = r'>='79t_EQ = r'=='80t_NE = r'!='8182# Assignment operators8384t_EQUALS = r'='85t_TIMESEQUAL = r'\*='86t_DIVEQUAL = r'/='87t_MODEQUAL = r'%='88t_PLUSEQUAL = r'\+='89t_MINUSEQUAL = r'-='90t_LSHIFTEQUAL = r'<<='91t_RSHIFTEQUAL = r'>>='92t_ANDEQUAL = r'&='93t_OREQUAL = r'\|='94t_XOREQUAL = r'^='9596# Increment/decrement97t_PLUSPLUS = r'\+\+'98t_MINUSMINUS = r'--'99100# ->101t_ARROW = r'->'102103# ?104t_CONDOP = r'\?'105106# Delimeters107t_LPAREN = r'\('108t_RPAREN = r'\)'109t_LBRACKET = r'\['110t_RBRACKET = r'\]'111t_LBRACE = r'\{'112t_RBRACE = r'\}'113t_COMMA = r','114t_PERIOD = r'\.'115t_SEMI = r';'116t_COLON = r':'117t_ELLIPSIS = r'\.\.\.'118119# Identifiers and reserved words120121reserved_map = { }122for r in reserved:123reserved_map[r.lower()] = r124125def t_ID(t):126r'[A-Za-z_][\w_]*'127t.type = reserved_map.get(t.value,"ID")128return t129130# Integer literal131t_ICONST = r'\d+([uU]|[lL]|[uU][lL]|[lL][uU])?'132133# Floating literal134t_FCONST = r'((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?'135136# String literal137t_SCONST = r'\"([^\\\n]|(\\.))*?\"'138139# Character constant 'c' or L'c'140t_CCONST = r'(L)?\'([^\\\n]|(\\.))*?\''141142# Comments143def t_comment(t):144r'/\*(.|\n)*?\*/'145t.lexer.lineno += t.value.count('\n')146147# Preprocessor directive (ignored)148def t_preprocessor(t):149r'\#(.)*?\n'150t.lexer.lineno += 1151152def t_error(t):153print("Illegal character %s" % repr(t.value[0]))154t.lexer.skip(1)155156lexer = lex.lex(optimize=1)157if __name__ == "__main__":158lex.runmain(lexer)159160161162163164165166