I’m trying to use Antlr4 to parse through an inclusion file (.inc) written in the syntax of fASM. This file may or may not contain other include statements. Here is an example snippet of such a file containing inclusion statements:
include 'Directory_EntryExportImage_Export_DirectoryImage_Export_Directory.inc'
include 'Directory_EntryImage_Import_Directory.inc'
include 'Directory_EntryImage_Resource_Directory.inc'
include 'Directory_EntryImage_Exception_Directory.inc'
include 'Directory_EntryImage_Security_Directory.inc'
include 'Directory_EntryImage_BaseReloc_Directory.inc'
include 'Directory_EntryImage_Debug_Directory.inc'
include 'Directory_EntryImage_Copyright_Directory.inc'
include 'Directory_EntryImage_GlobalPtr_Directory.inc'
include 'Directory_EntryImage_TLS_Directory.inc'
include 'Directory_EntryImage_LoadConfig_Directory.inc'
include 'Directory_EntryImage_BoundImport_Directory.inc'
include 'Directory_EntryImage_IAT_Directory.inc'
include 'Directory_EntryImage_DelayImport_Directory.inc'
include 'Directory_EntryImage_COMDescriptor_Directory.inc'
include 'Directory_EntryImage_Unused_Directory.inc'
; Contains actual values
struct PEHeader
mMagic dd ? ; // PE or 0x00004550
mMachine dw ?
mNumberOfSections dw ?
mTimeDateStamp dd ?
mPointerToSymbolTable dd ?
mNumberOfSymbols dd ?
mSizeOfOptionalHeader dw ?
mCharacteristics dw ?
ends
; Contains pointers to where those members would be
struct ptrPEHeader
ptrMagic dd 0
ptrMachine dd 0
ptrNumberOfSections dd 0
ptrTimeDateStamp dd 0
ptrPointerToSymbolTable dd 0
ptrNumberOfSymbols dd 0
ptrSizeOfOptionalHeader dd 0
ptrCharacteristics dd 0
ends
The problem is, I can’t seem to get Antlr4 to handle the inclusion lines correctly, it kind’ve chokes on them. Unfortunately, I do not have the output from when I was trying to parse the entire file; I only have the output of a small test file I made. Here is the contents of that file:
include 'Directory_EntryExportImage_Export_DirectoryImage_Export_Directory.inc'
variable0 equ 5
variable1 dw
struct TestStruct1
var1 db 10
var2 dw
MyOtherStruct var3
ends
Most importantly, here’s the contents of the grammar file I have so far:
grammar fASMtoCHeader;
//@lexer::header {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
//@parser::header {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
//@parser::listenerheader {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
//@parser::baselistenerheader {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
//@parser::visitorheader {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
//@parser::basevisitorheader {
// #include "/usr/local/include/antlr4-runtime/antlr4-runtime.h"
//}
// Lexer rules
STRUCT : 'struct';
ENDS : 'ends';
EQU : 'equ';
INCLUDE : 'include' ['"] [a-zA-Z0-9_-~]+'.'[iI][nN][cC]['"][rn]-> skip;
TYPE : 'db' | 'dw' | 'dd' | 'dq'; // Define built-in types (byte, word, double word, quad word)
ID : [a-zA-Z_][a-zA-Z0-9_]*;
NUMBER : [0-9]+;
COMMENT : ';' ~[rn]*; // Rule for comments
WS : [ trn]+ -> skip; // Ignore whitespace
// Parser rules
program : (includeStatement | equStatement | declStatement | structDefinition)* EOF;
includeStatement
: INCLUDE STRING
;
comment
: COMMENT
;
equStatement
: (ID EQU NUMBER)
;
declStatement
: ID type ( '?' ) COMMENT
| ID type ( '?' )
| ID type ( NUMBER ) COMMENT
| ID type ( NUMBER )
| ID type COMMENT
| ID type
| ID customType ( '?' ) COMMENT
| ID customType ( '?' )
| ID customType ( NUMBER ) COMMENT
| ID customType ( NUMBER )
| ID customType COMMENT
| ID customType
;
structDefinition
: STRUCT ID structMember* ENDS
;
structMember
: ID type ( '?' ) COMMENT
| ID type ( '?' )
| ID type ( NUMBER ) COMMENT
| ID type ( NUMBER )
| ID type COMMENT
| ID type
| ID customType ( '?' ) COMMENT
| ID customType ( '?' )
| ID customType ( NUMBER ) COMMENT
| ID customType ( NUMBER )
| ID customType COMMENT
| ID customType
;
type
: TYPE
;
customType
: ID
;
I’ve tried many different variations of handling the strings… I’ve tried
STRING : ''' ~[rn]* '''; // Based on rule for comments
I’ve tried breaking STRING up into SINGLEQUOTE string and DOUBLEQUOTE string, but the combination of the quotation marks (whether it’s single or double) with the un-escaped inside of the strings as part of the file paths seem to really throw Antlr4 for a loop. Since I did not know Antlr grammar file syntax (not the BNF style of content but more about how what is separated into what (i.e. what makes a line a lexer rule, and what makes it a parser rule), the grammar was initially created by me using ChatGPT and over many many iterations. The relevant content out of one of the iterations containing the single / double quote rules was this:
includeStatement: 'include' (SINGLE_QUOTE_STRING | DOUBLE_QUOTE_STRING) ;
SINGLE_QUOTE_STRING: ''' ('\' . | ~[\'])* ''' ;
DOUBLE_QUOTE_STRING: '"' ('\' . | ~[\"])* '"' ;
It had me trying things like this too:
STRING : '"' ( ~["\rn] | '\\' | '\"' )* '"'
| ''' ( ~[\'rn] | '\\' | '\'')* '''; // Handle single and double quotes
includeStatement
: INCLUDE STRING
;
To which, I would get errors like this:
line 6:8 token recognition error at: ''Directory_EntryE'
line 6:31 token recognition error at: ''
line 6:26 missing STRING at 'xport'
line 6:54 token recognition error at: ''
line 6:77 token recognition error at: '.'
line 6:55 mismatched input 'Image_Export_Directory' expecting {'=', ';'}
line 6:81 token recognition error at: ''r'
line 7:8 token recognition error at: ''Directory_EntryI'
line 7:0 mismatched input 'include' expecting {'=', ';'}
line 7:47 token recognition error at: '.'
line 7:26 missing STRING at 'mage_Import_Directory'
line 7:51 token recognition error at: ''r'
line 8:8 token recognition error at: ''Directory_EntryI'
line 8:0 mismatched input 'include' expecting {'=', ';'}
line 8:49 token recognition error at: '.'
line 8:26 missing STRING at 'mage_Resource_Directory'
line 8:53 token recognition error at: ''r'
line 9:8 token recognition error at: ''Directory_EntryI'
line 9:0 mismatched input 'include' expecting {'=', ';'}
line 9:50 token recognition error at: '.'
line 9:26 missing STRING at 'mage_Exception_Directory'
line 9:54 token recognition error at: ''r'
line 10:8 token recognition error at: ''Directory_EntryI'
line 10:0 mismatched input 'include' expecting {'=', ';'}
line 10:49 token recognition error at: '.'
line 10:26 missing STRING at 'mage_Security_Directory'
line 10:53 token recognition error at: ''r'
line 11:8 token recognition error at: ''Directory_EntryI'
line 11:0 mismatched input 'include' expecting {'=', ';'}
line 11:50 token recognition error at: '.'
line 11:26 missing STRING at 'mage_BaseReloc_Directory'
line 11:54 token recognition error at: ''r'
line 12:8 token recognition error at: ''Directory_EntryI'
line 12:0 mismatched input 'include' expecting {'=', ';'}
line 12:46 token recognition error at: '.'
line 12:26 missing STRING at 'mage_Debug_Directory'
line 12:50 token recognition error at: ''r'
Can anyone provide the necessary corrections to my grammar file so that, if possible, the include statements are handled (I can get at them in the C++ generated parser code that Antlr creates) or are at least skipped? Thanks!
StakFallT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.