IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Autres éditeurs Discussion :

[ANTLR] : quelques questions.


Sujet :

Autres éditeurs

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2004
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 89
    Par défaut [ANTLR] : quelques questions.
    Bonjour,

    Je débute en ANTLR et je compte sur vous pour éclaircir quelques points durs...

    Mon but est de parser des fichiers C pour en récupérer les typedef et structures...

    J'ai donc récupéré la grammaire C.g sur ANTLR que vous pouvez trouver en fin de message.

    Je travaille en C#.

    Question 1 :
    J 'ai quelques warnings à la génération des fichiers Lexer et Parser. Voici les logs :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    ANTLR Parser Generator  Version 3.0 (May 17, 2007)  1989-2007
    warning(200): C.g:468:38: Decision can match input such as "'else'" using multiple alternatives: 1, 2
    As a result, alternative(s) 2 were disabled for that input
    warning(200): C.g:517:4: Decision can match input such as "{'U', 'u'}{'L', 'l'}" using multiple alternatives: 1, 2
    As a result, alternative(s) 2 were disabled for that input
    warning(200): C.g:522:9: Decision can match input such as "'0'..'9'{'E', 'e'}{'+', '-'}'0'..'9'{'D', 'F', 'd', 'f'}" using multiple alternatives: 3, 4
    As a result, alternative(s) 4 were disabled for that input
    Quelqu'un a une idée ?


    Question 2 :
    J'aimerais afficher à chaque itération la chaine sur la laquelle on travaille. Comment puis je faire ?
    Il faut rajouter un {Console.WriteLIne()} dans la grammaire mais où et avec quoi comme paramètre ?

    Question 3 :
    Quelqu'un pourrait-il m'expliquer les deux extraitrs suivants car là je suis dépassé...
    Extrait 1 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    translation_unit
    	: external_declaration+
    	;
     
    external_declaration
    options {k=1;}
    	: ( declaration_specifiers? declarator declaration* '{' )=> function_definition
    	| declaration
    	;
    Extrait 2 :
    Là il commence à y avoir des ? et je ne comprends plus le fond de la règle...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    declaration
    scope {
      bool isTypedef;
    }
    @init {
      $declaration::isTypedef = false;
    }
    	: 'typedef' declaration_specifiers? {$declaration::isTypedef=true;}
    	  init_declarator_list ';' // special case, looking for typedef	
    	| declaration_specifiers init_declarator_list? ';'
    	;
     
    declaration_specifiers
    	:   (   storage_class_specifier
    		|   type_specifier
            |   type_qualifier
            )+
    	;
     
    init_declarator_list
    	: init_declarator (',' init_declarator)*
    	;

    Merci beaucoup !

    Pascal

    Annexe :

    Grammaire utilisée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
     
    /** ANSI C ANTLR v3 grammar
     
    Translated from Jutta Degener's 1995 ANSI C yacc grammar by Terence Parr
    July 2006.  The lexical rules were taken from the Java grammar.
     
    Jutta says: "In 1985, Jeff Lee published his Yacc grammar (which
    is accompanied by a matching Lex specification) for the April 30, 1985 draft
    version of the ANSI C standard.  Tom Stockfisch reposted it to net.sources in
    1987; that original, as mentioned in the answer to question 17.25 of the
    comp.lang.c FAQ, can be ftp'ed from ftp.uu.net,
       file usenet/net.sources/ansi.c.grammar.Z.
    I intend to keep this version as close to the current C Standard grammar as
    possible; please let me know if you discover discrepancies. Jutta Degener, 1995"
     
    Generally speaking, you need symbol table info to parse C; typedefs
    define types and then IDENTIFIERS are either types or plain IDs.  I'm doing
    the min necessary here tracking only type names.  This is a good example
    of the use of the global scope (called Symbols).  Every rule that declares its usage
    of Symbols pushes a new copy on the stack effectively creating a new
    symbol scope.  Also note rule declaration declares a rule scope that
    lets any invoked rule see isTypedef boolean.  It's much easier than
    passing that info down as parameters.  Very clean.  Rule
    direct_declarator can then easily determine whether the IDENTIFIER
    should be declared as a type name.
     
    I have only tested this on a single file, though it is 3500 lines.
     
    This grammar requires ANTLR v3 (3.0b3 or higher)
     
    Terence Parr
    July 2006
     
    ANTLR C# version - Kunle Odutola, November 2006
    */
    grammar C;
     
    options {
    	language=CSharp;
        backtrack=true;
        memoize=true;
        k=2;
    }
     
    scope Symbols {
    	IDictionary types;
    }
     
    @header {
    }
     
    @members {
    	bool isTypeName(string name) 
    	{
    		for (int i = Symbols_stack.Count-1; i>=0; i--) 
    		{
    			Symbols_scope scope = (Symbols_scope)Symbols_stack[i];
    			if ( scope.types.Contains(name) ) 
    			{
    				return true;
    			}
    		}
    		return false;
    	}
    	bool isfinStruct(string token) 
    	{
    		if (token.ToString() == "}")
    		{
    			return true;
    		}
    		return false;
    	}	
    }
     
    translation_unit
    scope Symbols; // entire file is a scope
    @init {
      $Symbols::types = new Hashtable();
    }
    	: external_declaration+
    	;
     
    /** Either a function definition or any other kind of C decl/def.
     *  The LL(*) analysis algorithm fails to deal with this due to
     *  recursion in the declarator rules.  I'm putting in a
     *  manual predicate here so that we don't backtrack over
     *  the entire function.  Further, you get a better error
     *  as errors within the function itself don't make it fail
     *  to predict that it's a function.  Weird errors previously.
     *  Remember: the goal is to avoid backtrack like the plague
     *  because it makes debugging, actions, and errors harder.
     *
     *  Note that k=1 results in a much smaller predictor for the 
     *  fixed lookahead; k=2 made a few extra thousand lines. ;)
     *  I'll have to optimize that in the future.
     */
    external_declaration
    options {k=1;}
    	: ( declaration_specifiers? declarator declaration* '{' )=> function_definition
    	| declaration
    	;
     
    function_definition
    scope Symbols; // put parameters and locals into same scope for now
    @init {
      $Symbols::types = new Hashtable();
    }
    	:	declaration_specifiers? declarator
    		(	declaration+ compound_statement	// K&R style
    		|	compound_statement				// ANSI style
    		)
    	;
     
    declaration
    scope {
      bool isTypedef;
    }
    @init {
      $declaration::isTypedef = false;
    }
    	: 'typedef' declaration_specifiers? {$declaration::isTypedef=true;}
    	  init_declarator_list ';' // special case, looking for typedef	
    	| declaration_specifiers init_declarator_list? ';'
    	;
     
    declaration_specifiers
    	:   (   storage_class_specifier
    		|   type_specifier
            |   type_qualifier
            )+
    	;
     
    init_declarator_list
    	: init_declarator (',' init_declarator)*
    	;
     
    init_declarator
    	: declarator ('=' initializer)?
    	;
     
    storage_class_specifier
    	: 'extern'		
    	| 'static'		
    	| 'auto'		
    	| 'register'	
    	;
     
    type_specifier
    	: 'void'		
    	| 'char'		
    	| 'short'		
    	| 'int'			
    	| 'long'		
    	| 'float'		
    	| 'double'		
    	| 'signed'		
    	| 'unsigned'	
    	| struct_or_union_specifier
    	| enum_specifier
    	| type_id
    	;
     
    type_id
        :   {isTypeName(input.LT(1).Text)}? IDENTIFIER
        	{Console.Out.WriteLine("\t" + input.LT(-1).Text + " " + input.LT(1).Text);}
        ;
     
    struct_or_union_specifier
    options {k=3;}
    scope Symbols; // structs are scopes
    @init {
      $Symbols::types = new Hashtable();
    }
    	: struct_or_union IDENTIFIER? '{' struct_declaration_list '}'
    	| struct_or_union IDENTIFIER
    	;
     
    struct_or_union
    	: 'struct'	{Console.Out.WriteLine("\r\nStruct\r\n{");}
    	| 'union'	{Console.Out.WriteLine("\r\nUnion\r\n{");}
    	;
     
    struct_declaration_list
    	: struct_declaration+
    	;
     
    struct_declaration
    	: specifier_qualifier_list struct_declarator_list ';'
    	;
     
    specifier_qualifier_list
    	: ( type_qualifier | type_specifier )+ 
    	;
     
    struct_declarator_list
    	: struct_declarator (',' struct_declarator)*
    	;
     
    struct_declarator
    	: declarator (':' constant_expression)?
    	| ':' constant_expression
    	;
     
    enum_specifier
    options {k=3;}
    	: 'enum' '{' enumerator_list '}'
    	| 'enum' IDENTIFIER '{' enumerator_list '}'
    	| 'enum' IDENTIFIER
    	;
     
    enumerator_list
    	: enumerator (',' enumerator)*
    	;
     
    enumerator
    	: IDENTIFIER ('=' constant_expression)?
    	;
     
    type_qualifier
    	: 'const'
    	| 'volatile'
    	;
     
    declarator
    	: pointer? direct_declarator
    	| pointer
    	;
     
    direct_declarator
    	:   (	IDENTIFIER
    			{
    			if ($declaration.Count>0 && $declaration::isTypedef) {
    				$Symbols::types[$IDENTIFIER.Text] = $IDENTIFIER.Text;
    				Console.Out.WriteLine("using " + input.LT(-1).Text + " = System." + input.LT(-2).Text + ";");
    			}
    			}
    		|	'(' declarator ')'
    		)
            declarator_suffix*
    	;
     
    declarator_suffix
    	:   '[' constant_expression ']'
        |   '[' ']'
        |   '(' parameter_type_list ')'
        |   '(' identifier_list ')'
        |   '(' ')'
    	;
     
    pointer
    	: '*' type_qualifier+ pointer?
    	| '*' pointer
    	| '*'
    	;
     
    parameter_type_list
    	: parameter_list (',' '...')?
    	;
     
    parameter_list
    	: parameter_declaration (',' parameter_declaration)*
    	;
     
    parameter_declaration
    	: declaration_specifiers (declarator|abstract_declarator)*
    	;
     
    identifier_list
    	: IDENTIFIER (',' IDENTIFIER)*
    	;
     
    type_name
    	: specifier_qualifier_list abstract_declarator?
    	;
     
    abstract_declarator
    	: pointer direct_abstract_declarator?
    	| direct_abstract_declarator
    	;
     
    direct_abstract_declarator
    	:	( '(' abstract_declarator ')' | abstract_declarator_suffix ) abstract_declarator_suffix*
    	;
     
    abstract_declarator_suffix
    	:	'[' ']'
    	|	'[' constant_expression ']'
    	|	'(' ')'
    	|	'(' parameter_type_list ')'
    	;
     
    initializer
    	: assignment_expression
    	| '{' initializer_list ','? '}'
    	;
     
    initializer_list
    	: initializer (',' initializer)*
    	;
     
    // E x p r e s s i o n s
     
    argument_expression_list
    	:   assignment_expression (',' assignment_expression)*
    	;
     
    additive_expression
    	: (multiplicative_expression) ('+' multiplicative_expression | '-' multiplicative_expression)*
    	;
     
    multiplicative_expression
    	: (cast_expression) ('*' cast_expression | '/' cast_expression | '%' cast_expression)*
    	;
     
    cast_expression
    	: '(' type_name ')' cast_expression
    	| unary_expression
    	;
     
    unary_expression
    	: postfix_expression
    	| '++' unary_expression
    	| '--' unary_expression
    	| unary_operator cast_expression
    	| 'sizeof' unary_expression
    	| 'sizeof' '(' type_name ')'
    	;
     
    postfix_expression
    	:   primary_expression
            (   '[' expression ']'
            |   '(' ')'
            |   '(' argument_expression_list ')'
            |   '.' IDENTIFIER
            |   '*' IDENTIFIER
            |   '->' IDENTIFIER
            |   '++'
            |   '--'
            )*
    	;
     
    unary_operator
    	: '&'
    	| '*'
    	| '+'
    	| '-'
    	| '~'
    	| '!'
    	;
     
    primary_expression
    	: IDENTIFIER
    	| constant
    	| '(' expression ')'
    	;
     
    constant
        :   HEX_LITERAL
        |   OCTAL_LITERAL
        |   DECIMAL_LITERAL
        |	CHARACTER_LITERAL
    	|	STRING_LITERAL
        |   FLOATING_POINT_LITERAL
        ;
     
    /////
     
    expression
    	: assignment_expression (',' assignment_expression)*
    	;
     
    constant_expression
    	: conditional_expression
    	;
     
    assignment_expression
    	: lvalue assignment_operator assignment_expression
    	| conditional_expression
    	;
     
    lvalue
    	:	unary_expression
    	;
     
    assignment_operator
    	: '='
    	| '*='
    	| '/='
    	| '%='
    	| '+='
    	| '-='
    	| '<<='
    	| '>>='
    	| '&='
    	| '^='
    	| '|='
    	;
     
    conditional_expression
    	: logical_or_expression ('?' expression ':' conditional_expression)?
    	;
     
    logical_or_expression
    	: logical_and_expression ('||' logical_and_expression)*
    	;
     
    logical_and_expression
    	: inclusive_or_expression ('&&' inclusive_or_expression)*
    	;
     
    inclusive_or_expression
    	: exclusive_or_expression ('|' exclusive_or_expression)*
    	;
     
    exclusive_or_expression
    	: and_expression ('^' and_expression)*
    	;
     
    and_expression
    	: equality_expression ('&' equality_expression)*
    	;
    equality_expression
    	: relational_expression (('=='|'!=') relational_expression)*
    	;
     
    relational_expression
    	: shift_expression (('<'|'>'|'<='|'>=') shift_expression)*
    	;
     
    shift_expression
    	: additive_expression (('<<'|'>>') additive_expression)*
    	;
     
    // S t a t e m e n t s
     
    statement
    	: labeled_statement
    	| compound_statement
    	| expression_statement
    	| selection_statement
    	| iteration_statement
    	| jump_statement
    	;
     
    labeled_statement
    	: IDENTIFIER ':' statement
    	| 'case' constant_expression ':' statement
    	| 'default' ':' statement
    	;
     
    compound_statement
    scope Symbols; // blocks have a scope of symbols
    @init {
      $Symbols::types = new Hashtable();
    }
    	: '{' declaration* statement_list? '}'
    	;
     
    statement_list
    	: statement+
    	;
     
    expression_statement
    	: ';'
    	| expression ';'
    	;
     
    selection_statement
    	: 'if' '(' expression ')' statement (options {k=1; backtrack=false;}:'else' statement)?
    	| 'switch' '(' expression ')' statement
    	;
     
    iteration_statement
    	: 'while' '(' expression ')' statement
    	| 'do' statement 'while' '(' expression ')' ';'
    	| 'for' '(' expression_statement expression_statement expression? ')' statement
    	;
     
    jump_statement
    	: 'goto' IDENTIFIER ';'
    	| 'continue' ';'
    	| 'break' ';'
    	| 'return' ';'
    	| 'return' expression ';'
    	;
     
    IDENTIFIER
    	:	LETTER (LETTER|'0'..'9')*
    	;
     
    fragment
    LETTER
    	:	'$'
    	|	'A'..'Z'
    	|	'a'..'z'
    	|	'_'
    	;
     
    CHARACTER_LITERAL
        :   '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
        ;
     
    STRING_LITERAL
        :  '"' ( EscapeSequence | ~('\\'|'"') )* '"'
        ;
     
    HEX_LITERAL : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
     
    DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
     
    OCTAL_LITERAL : '0' ('0'..'7')+ IntegerTypeSuffix? ;
     
    fragment
    HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
     
    fragment
    IntegerTypeSuffix
    	:	('u'|'U')? ('l'|'L')
    	|	('u'|'U')  ('l'|'L')?
    	;
     
    FLOATING_POINT_LITERAL
        :   ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
        |   '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
        |   ('0'..'9')+ Exponent FloatTypeSuffix?
        |   ('0'..'9')+ Exponent? FloatTypeSuffix
    	;
     
    fragment
    Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
     
    fragment
    FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
     
    fragment
    EscapeSequence
        :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
        |   OctalEscape
        ;
     
    fragment
    OctalEscape
        :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
        |   '\\' ('0'..'7') ('0'..'7')
        |   '\\' ('0'..'7')
        ;
     
    fragment
    UnicodeEscape
        :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit
        ;
     
    WS  :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
        ;
     
    COMMENT
        :   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
        ;
     
    LINE_COMMENT
        : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
        ;
     
    // ignore #line info for now
    LINE_COMMAND 
        : '#' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
        ;

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2004
    Messages
    89
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2004
    Messages : 89
    Par défaut Personne ?

Discussions similaires

  1. Quelques question sur Win 32 Appli
    Par lvdnono dans le forum Windows
    Réponses: 5
    Dernier message: 15/06/2004, 12h37
  2. [Débutant]Quelques questions de principe sur l'API win32
    Par silver_dragoon dans le forum Windows
    Réponses: 4
    Dernier message: 19/03/2004, 18h38
  3. [install]Install sous windows... quelques questions
    Par omega dans le forum Eclipse Java
    Réponses: 5
    Dernier message: 26/02/2004, 09h50
  4. [MFC] Quelques questions de débutant...
    Par Sephi dans le forum MFC
    Réponses: 4
    Dernier message: 20/02/2004, 17h25
  5. Quelques questions sur le TWebBrowser...
    Par CorO dans le forum Web & réseau
    Réponses: 3
    Dernier message: 17/01/2003, 21h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo