parser.py revision 11028:3a5190683bf2
1# Copyright (c) 2009 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import os.path
30import re
31import sys
32
33from m5.util import code_formatter
34from m5.util.grammar import Grammar, ParseError
35
36import slicc.ast as ast
37import slicc.util as util
38from slicc.symbols import SymbolTable
39
40class SLICC(Grammar):
41    def __init__(self, filename, base_dir, verbose=False, traceback=False, **kwargs):
42        self.protocol = None
43        self.traceback = traceback
44        self.verbose = verbose
45        self.symtab = SymbolTable(self)
46        self.base_dir = base_dir
47
48        try:
49            self.decl_list = self.parse_file(filename, **kwargs)
50        except ParseError, e:
51            if not self.traceback:
52                sys.exit(str(e))
53            raise
54
55    def currentLocation(self):
56        return util.Location(self.current_source, self.current_line,
57                             no_warning=not self.verbose)
58
59    def codeFormatter(self, *args, **kwargs):
60        code = code_formatter(*args, **kwargs)
61        code['protocol'] = self.protocol
62        return code
63
64    def process(self):
65        self.decl_list.findMachines()
66        self.decl_list.generate()
67
68    def writeCodeFiles(self, code_path, includes):
69        self.symtab.writeCodeFiles(code_path, includes)
70
71    def writeHTMLFiles(self, html_path):
72        self.symtab.writeHTMLFiles(html_path)
73
74    def files(self):
75        f = set([
76            'MachineType.cc',
77            'MachineType.hh',
78            'Types.hh' ])
79
80        f |= self.decl_list.files()
81
82        return f
83
84    t_ignore = '\t '
85
86    # C or C++ comment (ignore)
87    def t_c_comment(self, t):
88        r'/\*(.|\n)*?\*/'
89        t.lexer.lineno += t.value.count('\n')
90
91    def t_cpp_comment(self, t):
92        r'//.*'
93
94    # Define a rule so we can track line numbers
95    def t_newline(self, t):
96        r'\n+'
97        t.lexer.lineno += len(t.value)
98
99    reserved = {
100        'protocol' : 'PROTOCOL',
101        'include' : 'INCLUDE',
102        'global' : 'GLOBAL',
103        'machine' : 'MACHINE',
104        'in_port' : 'IN_PORT',
105        'out_port' : 'OUT_PORT',
106        'action' : 'ACTION',
107        'transition' : 'TRANS',
108        'structure' : 'STRUCT',
109        'external_type' : 'EXTERN_TYPE',
110        'enumeration' : 'ENUM',
111        'state_declaration' : 'STATE_DECL',
112        'peek' : 'PEEK',
113        'stall_and_wait' : 'STALL_AND_WAIT',
114        'enqueue' : 'ENQUEUE',
115        'check_allocate' : 'CHECK_ALLOCATE',
116        'check_next_cycle' : 'CHECK_NEXT_CYCLE',
117        'check_stop_slots' : 'CHECK_STOP_SLOTS',
118        'static_cast' : 'STATIC_CAST',
119        'if' : 'IF',
120        'is_valid' : 'IS_VALID',
121        'is_invalid' : 'IS_INVALID',
122        'else' : 'ELSE',
123        'return' : 'RETURN',
124        'void' : 'VOID',
125        'new' : 'NEW',
126        'OOD' : 'OOD',
127    }
128
129    literals = ':[]{}(),='
130
131    tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
132               'LEFTSHIFT', 'RIGHTSHIFT',
133               'NOT', 'AND', 'OR',
134               'PLUS', 'DASH', 'STAR', 'SLASH',
135               'INCR', 'DECR',
136               'DOUBLE_COLON', 'SEMI',
137               'ASSIGN', 'DOT',
138               'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
139    tokens += reserved.values()
140
141    t_EQ = r'=='
142    t_NE = r'!='
143    t_LT = r'<'
144    t_GT = r'>'
145    t_LE = r'<='
146    t_GE = r'>='
147    t_LEFTSHIFT = r'<<'
148    t_RIGHTSHIFT = r'>>'
149    t_NOT = r'!'
150    t_AND = r'&&'
151    t_OR = r'\|\|'
152    t_PLUS = r'\+'
153    t_DASH = r'-'
154    t_STAR = r'\*'
155    t_SLASH = r'/'
156    t_DOUBLE_COLON = r'::'
157    t_SEMI = r';'
158    t_ASSIGN = r':='
159    t_DOT = r'\.'
160    t_INCR = r'\+\+'
161    t_DECR = r'--'
162
163    precedence = (
164        ('left', 'INCR', 'DECR'),
165        ('left', 'AND', 'OR'),
166        ('left', 'EQ', 'NE'),
167        ('left', 'LT', 'GT', 'LE', 'GE'),
168        ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
169        ('left', 'PLUS', 'DASH'),
170        ('left', 'STAR', 'SLASH'),
171        ('right', 'NOT', 'UMINUS'),
172    )
173
174    def t_IDENT(self, t):
175        r'[a-zA-Z_][a-zA-Z_0-9]*'
176        if t.value == 'true':
177            t.type = 'LIT_BOOL'
178            t.value = True
179            return t
180
181        if t.value == 'false':
182            t.type = 'LIT_BOOL'
183            t.value = False
184            return t
185
186        # Check for reserved words
187        t.type = self.reserved.get(t.value, 'IDENT')
188        return t
189
190    def t_FLOATNUMBER(self, t):
191        '[0-9]+[.][0-9]+'
192        try:
193            t.value = float(t.value)
194        except ValueError:
195            raise ParseError("Illegal float", t)
196        return t
197
198    def t_NUMBER(self, t):
199        r'[0-9]+'
200        try:
201            t.value = int(t.value)
202        except ValueError:
203            raise ParseError("Illegal number", t)
204        return t
205
206    def t_STRING1(self, t):
207        r'\"[^"\n]*\"'
208        t.type = 'STRING'
209        t.value = t.value[1:-1]
210        return t
211
212    def t_STRING2(self, t):
213        r"\'[^'\n]*\'"
214        t.type = 'STRING'
215        t.value = t.value[1:-1]
216        return t
217
218    def p_file(self, p):
219        "file : decls"
220        p[0] = p[1]
221
222    def p_empty(self, p):
223        "empty :"
224
225    def p_decls(self, p):
226        "decls : declsx"
227        p[0] = ast.DeclListAST(self, p[1])
228
229    def p_declsx__list(self, p):
230        "declsx : decl declsx"
231        if isinstance(p[1], ast.DeclListAST):
232            decls = p[1].decls
233        elif p[1] is None:
234            decls = []
235        else:
236            decls = [ p[1] ]
237        p[0] = decls + p[2]
238
239    def p_declsx__none(self, p):
240        "declsx : empty"
241        p[0] = []
242
243    def p_decl__protocol(self, p):
244        "decl : PROTOCOL STRING SEMI"
245        if self.protocol:
246            msg = "Protocol can only be set once! Error at %s:%s\n" % \
247                (self.current_source, self.current_line)
248            raise ParseError(msg)
249        self.protocol = p[2]
250        p[0] = None
251
252    def p_decl__include(self, p):
253        "decl : INCLUDE STRING SEMI"
254        dirname = os.path.dirname(self.current_source)
255        if os.path.exists(os.path.join(dirname, p[2])):
256            filename = os.path.join(dirname, p[2])
257        else:
258            filename = os.path.join(self.base_dir, p[2])
259        p[0] = self.parse_file(filename)
260
261    def p_decl__machine0(self, p):
262        "decl : MACHINE '(' idents ')' ':' obj_decls '{' decls '}'"
263        p[0] = ast.MachineAST(self, p[3], [], p[7], p[9])
264
265    def p_decl__machine1(self, p):
266        "decl : MACHINE '(' idents pairs ')' ':' obj_decls '{' decls '}'"
267        p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
268
269    def p_decl__action(self, p):
270        "decl : ACTION '(' ident pairs ')' statements"
271        p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
272
273    def p_decl__in_port(self, p):
274        "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
275        p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
276
277    def p_decl__out_port(self, p):
278        "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
279        p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
280
281    def p_decl__trans0(self, p):
282        "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents"
283        p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], p[7], p[9])
284
285    def p_decl__trans1(self, p):
286        "decl : TRANS '(' idents ',' idents ')' idents"
287        p[0] = ast.TransitionDeclAST(self, [], p[3], p[5], None, p[7])
288
289    def p_decl__trans2(self, p):
290        "decl : TRANS '(' idents ',' idents ',' ident_or_star ')' idents idents"
291        p[0] = ast.TransitionDeclAST(self, p[9], p[3], p[5], p[7], p[10])
292
293    def p_decl__trans3(self, p):
294        "decl : TRANS '(' idents ',' idents ')' idents idents"
295        p[0] = ast.TransitionDeclAST(self, p[7], p[3], p[5], None, p[8])
296
297    def p_decl__extern0(self, p):
298        "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
299        p[4]["external"] = "yes"
300        p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
301
302    def p_decl__global(self, p):
303        "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
304        p[4]["global"] = "yes"
305        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
306
307    def p_decl__struct(self, p):
308        "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
309        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
310
311    def p_decl__enum(self, p):
312        "decl : ENUM '(' type pairs ')' '{' type_enums   '}'"
313        p[4]["enumeration"] = "yes"
314        p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
315
316    def p_decl__state_decl(self, p):
317        "decl : STATE_DECL '(' type pairs ')' '{' type_states   '}'"
318        p[4]["enumeration"] = "yes"
319        p[4]["state_decl"] = "yes"
320        p[0] = ast.StateDeclAST(self, p[3], p[4], p[7])
321
322    # Type fields
323    def p_obj_decls__list(self, p):
324        "obj_decls : obj_decl obj_decls"
325        p[0] = [ p[1] ] + p[2]
326
327    def p_obj_decls__empty(self, p):
328        "obj_decls : empty"
329        p[0] = []
330
331    def p_type_members__list(self, p):
332        "type_members : type_member type_members"
333        p[0] = [ p[1] ] + p[2]
334
335    def p_type_members__empty(self, p):
336        "type_members : empty"
337        p[0] = []
338
339    def p_type_member__0(self, p):
340        """type_member : obj_decl
341                       | func_decl
342                       | func_def"""
343        p[0] = p[1]
344
345    # Member / Variable declarations
346    def p_decl__obj_decl(self, p):
347        "decl : obj_decl"
348        p[0] = p[1]
349
350    def p_obj_decl__0(self, p):
351        "obj_decl : type ident pairs SEMI"
352        p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3], None, False)
353
354    def p_obj_decl__1(self, p):
355        "obj_decl : type STAR ident pairs SEMI"
356        p[0] = ast.ObjDeclAST(self, p[1], p[3], p[4], None, True)
357
358    def p_obj_decl__2(self, p):
359        "obj_decl : type ident ASSIGN expr SEMI"
360        p[0] = ast.ObjDeclAST(self, p[1], p[2], ast.PairListAST(self), p[4],
361                False)
362
363    def p_obj_decl__3(self, p):
364        "obj_decl : type STAR ident ASSIGN expr SEMI"
365        p[0] = ast.ObjDeclAST(self, p[1], p[3], ast.PairListAST(self), p[5],
366                True)
367
368    # Function definition and declaration
369    def p_decl__func_decl(self, p):
370        "decl : func_decl"
371        p[0] = p[1]
372
373    def p_func_decl__0(self, p):
374        """func_decl :  void ident '(' params ')' pairs SEMI
375                | type ident '(' params ')' pairs SEMI"""
376        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
377
378    def p_func_decl__1(self, p):
379        """func_decl :  void ident '(' types ')' pairs SEMI
380                | type ident '(' types ')' pairs SEMI"""
381        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
382
383    def p_decl__func_def(self, p):
384        "decl : func_def"
385        p[0] = p[1]
386
387    def p_func_def__0(self, p):
388        """func_def : void ident '(' params ')' pairs statements
389            | type ident '(' params ')' pairs statements"""
390        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
391
392    # Enum fields
393    def p_type_enums__list(self, p):
394        "type_enums : type_enum type_enums"
395        p[0] = [ p[1] ] + p[2]
396
397    def p_type_enums__empty(self, p):
398        "type_enums : empty"
399        p[0] = []
400
401    def p_type_enum(self, p):
402        "type_enum : ident pairs SEMI"
403        p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
404
405    # States
406    def p_type_states__list(self, p):
407        "type_states : type_state type_states"
408        p[0] = [ p[1] ] + p[2]
409
410    def p_type_states__empty(self, p):
411        "type_states : empty"
412        p[0] = []
413
414    def p_type_state(self, p):
415        "type_state : ident ',' enumeration pairs SEMI"
416        p[0] = ast.TypeFieldStateAST(self, p[1], p[3], p[4])
417
418    # Formal Param
419    def p_params__many(self, p):
420        "params : param ',' params"
421        p[0] = [ p[1] ] + p[3]
422
423    def p_params__one(self, p):
424        "params : param"
425        p[0] = [ p[1] ]
426
427    def p_params__none(self, p):
428        "params : empty"
429        p[0] = []
430
431    def p_param(self, p):
432        "param : type ident"
433        p[0] = ast.FormalParamAST(self, p[1], p[2])
434
435    def p_param__pointer(self, p):
436        "param : type STAR ident"
437        p[0] = ast.FormalParamAST(self, p[1], p[3], None, True)
438
439    def p_param__pointer_default(self, p):
440        "param : type STAR ident ASSIGN STRING"
441        p[0] = ast.FormalParamAST(self, p[1], p[3], p[5], True)
442
443    def p_param__default_number(self, p):
444        "param : type ident ASSIGN NUMBER"
445        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
446
447    def p_param__default_bool(self, p):
448        "param : type ident ASSIGN LIT_BOOL"
449        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
450
451    def p_param__default_string(self, p):
452        "param : type ident ASSIGN STRING"
453        p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
454
455    # Type
456    def p_types__multiple(self, p):
457        "types : type ',' types"
458        p[0] = [ p[1] ] + p[3]
459
460    def p_types__one(self, p):
461        "types : type"
462        p[0] = [ p[1] ]
463
464    def p_types__empty(self, p):
465        "types : empty"
466        p[0] = []
467
468    def p_typestr__multi(self, p):
469        "typestr : typestr DOUBLE_COLON ident"
470        p[0] = '%s::%s' % (p[1], p[3])
471
472    def p_typestr__single(self, p):
473        "typestr : ident"
474        p[0] = p[1]
475
476    def p_type__one(self, p):
477        "type : typestr"
478        p[0] = ast.TypeAST(self, p[1])
479
480    def p_void(self, p):
481        "void : VOID"
482        p[0] = ast.TypeAST(self, p[1])
483
484    # Idents and lists
485    def p_idents__braced(self, p):
486        "idents : '{' identx '}'"
487        p[0] = p[2]
488
489    def p_idents__bare(self, p):
490        "idents : ident"
491        p[0] = [ p[1] ]
492
493    def p_identx__multiple_1(self, p):
494        """identx : ident SEMI identx
495                  | ident ',' identx"""
496        p[0] = [ p[1] ] + p[3]
497
498    def p_identx__multiple_2(self, p):
499        "identx : ident identx"
500        p[0] = [ p[1] ] + p[2]
501
502    def p_identx__single(self, p):
503        "identx : empty"
504        p[0] = [ ]
505
506    def p_ident(self, p):
507        "ident : IDENT"
508        p[0] = p[1]
509
510    def p_ident_or_star(self, p):
511        """ident_or_star : ident
512                         | STAR"""
513        p[0] = p[1]
514
515    # Pair and pair lists
516    def p_pairs__list(self, p):
517        "pairs : ',' pairsx"
518        p[0] = p[2]
519
520    def p_pairs__empty(self, p):
521        "pairs : empty"
522        p[0] = ast.PairListAST(self)
523
524    def p_pairsx__many(self, p):
525        "pairsx : pair ',' pairsx"
526        p[0] = p[3]
527        p[0].addPair(p[1])
528
529    def p_pairsx__one(self, p):
530        "pairsx : pair"
531        p[0] = ast.PairListAST(self)
532        p[0].addPair(p[1])
533
534    def p_pair__assign(self, p):
535        """pair : ident '=' STRING
536                | ident '=' ident
537                | ident '=' NUMBER"""
538        p[0] = ast.PairAST(self, p[1], p[3])
539
540    def p_pair__literal(self, p):
541        "pair : STRING"
542        p[0] = ast.PairAST(self, "short", p[1])
543
544    # Below are the rules for action descriptions
545    def p_statements__inner(self, p):
546        "statements : '{' statements_inner '}'"
547        p[0] = ast.StatementListAST(self, p[2])
548
549    def p_statements__none(self, p):
550        "statements : '{' '}'"
551        p[0] = ast.StatementListAST(self, [])
552
553    def p_statements_inner__many(self, p):
554        "statements_inner : statement statements_inner"
555        p[0] = [ p[1] ] + p[2]
556
557    def p_statements_inner__one(self, p):
558        "statements_inner : statement"
559        p[0] = [ p[1] ]
560
561    def p_exprs__multiple(self, p):
562        "exprs : expr ',' exprs"
563        p[0] = [ p[1] ] + p[3]
564
565    def p_exprs__one(self, p):
566        "exprs : expr"
567        p[0] = [ p[1] ]
568
569    def p_exprs__empty(self, p):
570        "exprs : empty"""
571        p[0] = []
572
573    def p_statement__expression(self, p):
574        "statement : expr SEMI"
575        p[0] = ast.ExprStatementAST(self, p[1])
576
577    def p_statement__assign(self, p):
578        "statement : expr ASSIGN expr SEMI"
579        p[0] = ast.AssignStatementAST(self, p[1], p[3])
580
581    def p_statement__enqueue(self, p):
582        "statement : ENQUEUE '(' var ',' type ')' statements"
583        p[0] = ast.EnqueueStatementAST(self, p[3], p[5], None, p[7])
584
585    def p_statement__enqueue_latency(self, p):
586        "statement : ENQUEUE '(' var ',' type ',' expr ')' statements"
587        p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[7], p[9])
588
589    def p_statement__stall_and_wait(self, p):
590        "statement : STALL_AND_WAIT '(' var ',' var ')' SEMI"
591        p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5])
592
593    def p_statement__peek(self, p):
594        "statement : PEEK '(' var ',' type pairs ')' statements"
595        p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek")
596
597    def p_statement__check_allocate(self, p):
598        "statement : CHECK_ALLOCATE '(' var ')' SEMI"
599        p[0] = ast.CheckAllocateStatementAST(self, p[3])
600
601    def p_statement__check_next_cycle(self, p):
602        "statement : CHECK_NEXT_CYCLE '(' ')' SEMI"
603        p[0] = ast.CheckNextCycleAST(self)
604
605    def p_statement__check_stop(self, p):
606        "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI"
607        p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
608
609    def p_statement__return(self, p):
610        "statement : RETURN expr SEMI"
611        p[0] = ast.ReturnStatementAST(self, p[2])
612
613    def p_statement__if(self, p):
614        "statement : if_statement"
615        p[0] = p[1]
616
617    def p_if_statement__if(self, p):
618        "if_statement : IF '(' expr ')' statements"
619        p[0] = ast.IfStatementAST(self, p[3], p[5], None)
620
621    def p_if_statement__if_else(self, p):
622        "if_statement : IF '(' expr ')' statements ELSE statements"
623        p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
624
625    def p_statement__if_else_if(self, p):
626        "if_statement : IF '(' expr ')' statements ELSE if_statement"
627        p[0] = ast.IfStatementAST(self, p[3], p[5],
628                                  ast.StatementListAST(self, p[7]))
629
630    def p_expr__static_cast(self, p):
631        "aexpr : STATIC_CAST '(' type ',' expr ')'"
632        p[0] = ast.StaticCastAST(self, p[3], "ref", p[5])
633
634    def p_expr__static_cast_ptr(self, p):
635        "aexpr : STATIC_CAST '(' type ',' STRING ',' expr ')'"
636        p[0] = ast.StaticCastAST(self, p[3], p[5], p[7])
637
638    def p_expr__var(self, p):
639        "aexpr : var"
640        p[0] = p[1]
641
642    def p_expr__localvar(self, p):
643        "aexpr : type ident"
644        p[0] = ast.LocalVariableAST(self, p[1], p[2])
645
646    def p_expr__literal(self, p):
647        "aexpr : literal"
648        p[0] = p[1]
649
650    def p_expr__enumeration(self, p):
651        "aexpr : enumeration"
652        p[0] = p[1]
653
654    def p_expr__func_call(self, p):
655        "aexpr : ident '(' exprs ')'"
656        p[0] = ast.FuncCallExprAST(self, p[1], p[3])
657
658    def p_expr__new(self, p):
659        "aexpr : NEW type"
660        p[0] = ast.NewExprAST(self, p[2])
661
662    def p_expr__null(self, p):
663        "aexpr : OOD"
664        p[0] = ast.OodAST(self)
665
666    def p_expr__member(self, p):
667        "aexpr : aexpr DOT ident"
668        p[0] = ast.MemberExprAST(self, p[1], p[3])
669
670    def p_expr__member_method_call(self, p):
671        "aexpr : aexpr DOT ident '(' exprs ')'"
672        p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
673
674    def p_expr__class_method_call(self, p):
675        "aexpr : type DOUBLE_COLON ident '(' exprs ')'"
676        p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
677
678    def p_expr__aexpr(self, p):
679        "expr : aexpr"
680        p[0] = p[1]
681
682    def p_expr__binary_op(self, p):
683        """expr : expr STAR  expr
684                | expr SLASH expr
685                | expr PLUS  expr
686                | expr DASH  expr
687                | expr LT    expr
688                | expr GT    expr
689                | expr LE    expr
690                | expr GE    expr
691                | expr EQ    expr
692                | expr NE    expr
693                | expr AND   expr
694                | expr OR    expr
695                | expr RIGHTSHIFT expr
696                | expr LEFTSHIFT  expr"""
697        p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
698
699    # FIXME - unary not
700    def p_expr__unary_op(self, p):
701        """expr : NOT expr
702                | INCR expr
703                | DECR expr
704                | DASH expr %prec UMINUS"""
705        p[0] = ast.PrefixOperatorExprAST(self, p[1], p[2])
706
707    def p_expr__parens(self, p):
708        "aexpr : '(' expr ')'"
709        p[0] = p[2]
710
711    def p_expr__is_valid_ptr(self, p):
712        "aexpr : IS_VALID '(' var ')'"
713        p[0] = ast.IsValidPtrExprAST(self, p[3], True)
714
715    def p_expr__is_invalid_ptr(self, p):
716        "aexpr : IS_INVALID '(' var ')'"
717        p[0] = ast.IsValidPtrExprAST(self, p[3], False)
718
719    def p_literal__string(self, p):
720        "literal : STRING"
721        p[0] = ast.LiteralExprAST(self, p[1], "std::string")
722
723    def p_literal__number(self, p):
724        "literal : NUMBER"
725        p[0] = ast.LiteralExprAST(self, p[1], "int")
726
727    def p_literal__float(self, p):
728        "literal : FLOATNUMBER"
729        p[0] = ast.LiteralExprAST(self, p[1], "int")
730
731    def p_literal__bool(self, p):
732        "literal : LIT_BOOL"
733        p[0] = ast.LiteralExprAST(self, p[1], "bool")
734
735    def p_enumeration(self, p):
736        "enumeration : ident ':' ident"
737        p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
738
739    def p_var(self, p):
740        "var : ident"
741        p[0] = ast.VarExprAST(self, p[1])
742