2 This file is used to be the grammar file of ECC tool
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 # The file defines the parser for C source files.
24 # THIS FILE IS AUTO-GENENERATED. PLEASE DON NOT MODIFY THIS FILE.
25 # This file is generated by running:
26 # java org.antlr.Tool C.g
28 # Copyright (c) 2009 - 2010, Intel Corporation All rights reserved.
30 # This program and the accompanying materials are licensed and made available
31 # under the terms and conditions of the BSD License which accompanies this
32 # distribution. The full text of the license may be found at:
33 # http://opensource.org/licenses/bsd-license.php
35 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
36 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
40 import Ecc.CodeFragment as CodeFragment
41 import Ecc.FileProfile as FileProfile
46 def printTokenInfo(self, line, offset, tokenText):
47 print(str(line)+ ',' + str(offset) + ':' + str(tokenText))
49 def StorePredicateExpression(self, StartLine, StartOffset, EndLine, EndOffset, Text):
50 PredExp = CodeFragment.PredicateExpression(Text, (StartLine, StartOffset), (EndLine, EndOffset))
51 FileProfile.PredicateExpressionList.append(PredExp)
53 def StoreEnumerationDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
54 EnumDef = CodeFragment.EnumerationDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
55 FileProfile.EnumerationDefinitionList.append(EnumDef)
57 def StoreStructUnionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, Text):
58 SUDef = CodeFragment.StructUnionDefinition(Text, (StartLine, StartOffset), (EndLine, EndOffset))
59 FileProfile.StructUnionDefinitionList.append(SUDef)
61 def StoreTypedefDefinition(self, StartLine, StartOffset, EndLine, EndOffset, FromText, ToText):
62 Tdef = CodeFragment.TypedefDefinition(FromText, ToText, (StartLine, StartOffset), (EndLine, EndOffset))
63 FileProfile.TypedefDefinitionList.append(Tdef)
65 def StoreFunctionDefinition(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText, LeftBraceLine, LeftBraceOffset, DeclLine, DeclOffset):
66 FuncDef = CodeFragment.FunctionDefinition(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset), (LeftBraceLine, LeftBraceOffset), (DeclLine, DeclOffset))
67 FileProfile.FunctionDefinitionList.append(FuncDef)
69 def StoreVariableDeclaration(self, StartLine, StartOffset, EndLine, EndOffset, ModifierText, DeclText):
70 VarDecl = CodeFragment.VariableDeclaration(ModifierText, DeclText, (StartLine, StartOffset), (EndLine, EndOffset))
71 FileProfile.VariableDeclarationList.append(VarDecl)
73 def StoreFunctionCalling(self, StartLine, StartOffset, EndLine, EndOffset, FuncName, ParamList):
74 FuncCall = CodeFragment.FunctionCalling(FuncName, ParamList, (StartLine, StartOffset), (EndLine, EndOffset))
75 FileProfile.FunctionCallingList.append(FuncCall)
80 : external_declaration*
85 : ( declaration_specifiers? declarator declaration* '{' )
88 | macro_statement (';')?
92 locals [String ModifierText = '', String DeclText = '', int LBLine = 0, int LBOffset = 0, int DeclLine = 0, int DeclOffset = 0]
102 self.StoreFunctionDefinition(localctx.start.line, localctx.start.column, localctx.stop.line, localctx.stop.column, ModifierText, DeclText, LBLine, LBOffset, DeclLine, DeclOffset)
104 : d=declaration_specifiers? declarator
105 ( declaration+ a=compound_statement // K&R style
106 | b=compound_statement // ANSI style
108 if localctx.d != None:
109 ModifierText = $declaration_specifiers.text
112 DeclText = $declarator.text
113 DeclLine = $declarator.start.line
114 DeclOffset = $declarator.start.column
115 if localctx.a != None:
116 LBLine = $a.start.line
117 LBOffset = $a.start.column
119 LBLine = $b.start.line
120 LBOffset = $b.start.column
125 declaration_specifiers
126 : ( storage_class_specifier
133 : a='typedef' b=declaration_specifiers? c=init_declarator_list d=';'
135 if localctx.b is not None:
136 self.StoreTypedefDefinition(localctx.a.line, localctx.a.column, $d.line, localctx.d.column, $b.text, $c.text)
138 self.StoreTypedefDefinition(localctx.a.line, localctx.a.column, $d.line, localctx.d.column, '', $c.text)
140 | s=declaration_specifiers t=init_declarator_list? e=';'
142 if localctx.t is not None:
143 self.StoreVariableDeclaration($s.start.line, $s.start.column, $t.start.line, $t.start.column, $s.text, $t.text)
148 : init_declarator (',' init_declarator)*
152 : declarator ('=' initializer)?
155 storage_class_specifier
173 | s=struct_or_union_specifier
175 if localctx.s.stop is not None:
176 self.StoreStructUnionDefinition($s.start.line, $s.start.column, $s.stop.line, $s.stop.column, $s.text)
180 if localctx.e.stop is not None:
181 self.StoreEnumerationDefinition($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)
183 | (IDENTIFIER type_qualifier* declarator)
189 //{self.printTokenInfo($a.line, $a.pos, $a.text)}
192 struct_or_union_specifier
193 : struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
194 | struct_or_union IDENTIFIER
202 struct_declaration_list
203 : struct_declaration+
207 : specifier_qualifier_list struct_declarator_list ';'
210 specifier_qualifier_list
211 : ( type_qualifier | type_specifier )+
214 struct_declarator_list
215 : struct_declarator (',' struct_declarator)*
219 : declarator (':' constant_expression)?
220 | ':' constant_expression
224 : 'enum' '{' enumerator_list ','? '}'
225 | 'enum' IDENTIFIER '{' enumerator_list ','? '}'
230 : enumerator (',' enumerator)*
234 : IDENTIFIER ('=' constant_expression)?
246 | 'GLOBAL_REMOVE_IF_UNREFERENCED'
249 | 'EFI_RUNTIMESERVICE'
254 : pointer? ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? direct_declarator
255 // | ('EFIAPI')? ('EFI_BOOTSERVICE')? ('EFI_RUNTIMESERVICE')? pointer? direct_declarator
260 : IDENTIFIER declarator_suffix*
261 | '(' ('EFIAPI')? declarator ')' declarator_suffix+
265 : '[' constant_expression ']'
267 | '(' parameter_type_list ')'
268 | '(' identifier_list ')'
273 : '*' type_qualifier+ pointer?
279 : parameter_list (',' ('OPTIONAL')? '...')?
283 : parameter_declaration (',' ('OPTIONAL')? parameter_declaration)*
286 parameter_declaration
287 : declaration_specifiers (declarator|abstract_declarator)* ('OPTIONAL')?
288 //accomerdate user-defined type only, no declarator follow.
289 | pointer* IDENTIFIER
298 : specifier_qualifier_list abstract_declarator?
303 : pointer direct_abstract_declarator?
304 | direct_abstract_declarator
307 direct_abstract_declarator
308 : ( '(' abstract_declarator ')' | abstract_declarator_suffix ) abstract_declarator_suffix*
311 abstract_declarator_suffix
313 | '[' constant_expression ']'
315 | '(' parameter_type_list ')'
320 : assignment_expression
321 | '{' initializer_list ','? '}'
325 : initializer (',' initializer )*
328 // E x p r e s s i o n s
330 argument_expression_list
331 : assignment_expression ('OPTIONAL')? (',' assignment_expression ('OPTIONAL')?)*
335 : (multiplicative_expression) ('+' multiplicative_expression | '-' multiplicative_expression)*
338 multiplicative_expression
339 : (cast_expression) ('*' cast_expression | '/' cast_expression | '%' cast_expression)*
343 : '(' type_name ')' cast_expression
349 | '++' unary_expression
350 | '--' unary_expression
351 | unary_operator cast_expression
352 | 'sizeof' unary_expression
353 | 'sizeof' '(' type_name ')'
357 locals [FuncCallText='']
362 : p=primary_expression {self.FuncCallText += $p.text}
364 | '(' a=')'{self.StoreFunctionCalling($p.start.line, $p.start.column, $a.line, localctx.a.column, self.FuncCallText, '')}
365 | '(' c=argument_expression_list b=')' {self.StoreFunctionCalling($p.start.line, $p.start.column, $b.line, localctx.b.column, self.FuncCallText, $c.text)}
366 | '(' macro_parameter_list ')'
367 | '.' x=IDENTIFIER {self.FuncCallText += '.' + $x.text}
368 | '*' y=IDENTIFIER {self.FuncCallText = $y.text}
369 | '->' z=IDENTIFIER {self.FuncCallText += '->' + $z.text}
376 : parameter_declaration (',' parameter_declaration)*
399 | (IDENTIFIER* STRING_LITERAL+)+ IDENTIFIER*
400 | FLOATING_POINT_LITERAL
406 : assignment_expression (',' assignment_expression)*
410 : conditional_expression
413 assignment_expression
414 : lvalue assignment_operator assignment_expression
415 | conditional_expression
436 conditional_expression
437 : e=logical_or_expression ('?' expression ':' conditional_expression {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)})?
440 logical_or_expression
441 : logical_and_expression ('||' logical_and_expression)*
444 logical_and_expression
445 : inclusive_or_expression ('&&' inclusive_or_expression)*
448 inclusive_or_expression
449 : exclusive_or_expression ('|' exclusive_or_expression)*
452 exclusive_or_expression
453 : and_expression ('^' and_expression)*
457 : equality_expression ('&' equality_expression)*
460 : relational_expression (('=='|'!=') relational_expression )*
463 relational_expression
464 : shift_expression (('<'|'>'|'<='|'>=') shift_expression)*
468 : additive_expression (('<<'|'>>') additive_expression)*
471 // S t a t e m e n t s
476 | expression_statement
477 | selection_statement
478 | iteration_statement
488 : '__asm__'? IDENTIFIER '(' (~(';'))* ')' ';'
492 : '_asm' '{' (~('}'))* '}'
496 : '__asm' '{' (~('}'))* '}'
500 : IDENTIFIER '(' declaration* statement_list? expression? ')'
504 : IDENTIFIER ':' statement
505 | 'case' constant_expression ':' statement
506 | 'default' ':' statement
510 : '{' declaration* statement_list? '}'
523 : 'if' '(' e=expression ')' {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)} statement (:'else' statement)?
524 | 'switch' '(' expression ')' statement
528 : 'while' '(' e=expression ')' statement {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
529 | 'do' statement 'while' '(' e=expression ')' ';' {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
530 //| 'for' '(' expression_statement e=expression_statement expression? ')' statement {self.StorePredicateExpression($e.start.line, $e.start.column, $e.stop.line, $e.stop.column, $e.text)}
534 : 'goto' IDENTIFIER ';'
538 | 'return' expression ';'
542 : LETTER (LETTER|'0'..'9')*
554 : ('L')? '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
558 : ('L')? '"' ( EscapeSequence | ~('\\'|'"') )* '"'
561 HEX_LITERAL : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
563 DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
565 OCTAL_LITERAL : '0' ('0'..'7')+ IntegerTypeSuffix? ;
568 HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
574 | ('u'|'U') ('l'|'L')
575 | ('u'|'U') ('l'|'L') ('l'|'L')
578 FLOATING_POINT_LITERAL
579 : ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
580 | '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
581 | ('0'..'9')+ Exponent FloatTypeSuffix?
582 | ('0'..'9')+ Exponent? FloatTypeSuffix
586 Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
589 FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
593 : '\\' ('b'|'t'|'n'|'f'|'r'|'\''|'\\')
599 : '\\' ('0'..'3') ('0'..'7') ('0'..'7')
600 | '\\' ('0'..'7') ('0'..'7')
606 : '\\' 'u' HexDigit HexDigit HexDigit HexDigit
609 WS : (' '|'\r'|'\t'|'\u000C'|'\n')
613 // ingore '\' of line concatenation
628 : '//' ~('\n'|'\r')* '\r'? '\n'
632 // ignore #line info for now
634 : '#' ~('\n'|'\r')* '\r'? '\n'