Using Antlr4 to parse an fASM inclusion file (.inc), handle inclusion statements, and if it’s possible with Antlr4 (handle it), if it’s not (skip it)

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!

New contributor

StakFallT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật