antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.11k stars 3.28k forks source link

unable to parse correctly #2272

Open peterremote1980 opened 6 years ago

peterremote1980 commented 6 years ago

Hi all

I am using antlr4 and i want to parse this:

```
(bits 64){
    aaa
}
```

IF i delete this line "code16_32 : 'aaa'" in my grammar, it can parse, why? It shouldn't jump into rule "code16_32". Please help !!!

My grammar is :

```

grammar Assembler;

WS      :   [ \t]+ -> skip;
NL      :   '\r'? '\n' -> skip;
LINE_COMMENT    :   ';' ~[\r\n]*;
INSTRUCTION :   [a-z]+;
IDENTIFIER  :   [a-zA-Z] [a-zA-Z0-9]+;
SECTION_NAME    :   '.' IDENTIFIER;
LABEL_POSTFIX   :   'equ' ~[;]*;

assemble    :   lines
            |   EOF
            ;

lines       :   line*
            ;

line        :   bits64
            ;

comment     :   LINE_COMMENT
            ;

code16_32_64        :   'DUMMY'
                    ;

code16      :   'DUMMY'
            ;

code16_32   :   'aaa'                       # aaa
            |   'aad'                       # aad
            |   'aad' IMM8                  # aad_imm8
            |   'aam'                       # aam
            |   'aam' IMM8                  # aam_imm8
            |   'aas'                       # aas
            |   'adc' AL ',' IMM8           # adc_imm8
            |   'adc' AX ',' IMM16          # adc_imm16
            ;

code32      :   INSTRUCTION
            ;

code32_64   :   INSTRUCTION
            ;

code64      :   INSTRUCTION {System.err.println("fuck");}
            ;

data        :   'db' STRING ',' NUMBER
            ;

bits64      :   '(bits 64){' (code64 | code32_64 | code16_32_64)* comment? '}'
            ;


Thanks
From Peter
ericvergnaud commented 6 years ago

Hi,

the place for support is the google discussion group. Please close this.

Eric

Le 9 avr. 2018 à 01:54, 火雲邪神 notifications@github.com a écrit :

Hi all

I am using antlr4 and i want to parse this:

(bits 64){
  aaa
}

IF i delete this line "code16_32 : 'aaa'" in my grammar, it can parse, why? It shouldn't jump into rule "code16_32". Please help !!!

My grammar is :


grammar Assembler;

WS        :   [ \t]+ -> skip;
NL        :   '\r'? '\n' -> skip;
LINE_COMMENT  :   ';' ~[\r\n]*;
INSTRUCTION   :   [a-z]+;
IDENTIFIER    :   [a-zA-Z] [a-zA-Z0-9]+;
SECTION_NAME  :   '.' IDENTIFIER;
LABEL_POSTFIX :   'equ' ~[;]*;

assemble  :   lines
          |   EOF
          ;

lines     :   line*
          ;

line      :   bits64
          ;

comment       :   LINE_COMMENT
          ;

code16_32_64      :   'DUMMY'
                  ;

code16        :   'DUMMY'
          ;

code16_32 :   'aaa'                       # aaa
          |   'aad'                       # aad
          |   'aad' IMM8                  # aad_imm8
          |   'aam'                       # aam
          |   'aam' IMM8                  # aam_imm8
          |   'aas'                       # aas
          |   'adc' AL ',' IMM8           # adc_imm8
          |   'adc' AX ',' IMM16          # adc_imm16
          ;

code32        :   INSTRUCTION
          ;

code32_64 :   INSTRUCTION
          ;

code64        :   INSTRUCTION {System.err.println("fuck");}
          ;

data      :   'db' STRING ',' NUMBER
          ;

bits64        :   '(bits 64){' (code64 | code32_64 | code16_32_64)* comment? '}'
          ;

Thanks
From Peter
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <https://github.com/antlr/antlr4/issues/2272>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ADLYJJpyS0RzhRWOk7v1ZoqubGk7671Wks5tmk7egaJpZM4TLodw>.