HobbyOSs / opennask

nask clone assembly, it can boot tiny OS with Linux
https://github.com/HobbyOSs/opennask/wiki/%E5%8B%95%E4%BD%9C%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF
GNU General Public License v3.0
20 stars 3 forks source link

BNFCで生成したソースを使う #37

Closed hangingman closed 3 years ago

hangingman commented 3 years ago

ref #32

まだ一部修正の余地はあるが、BNFCで作成したBNFは以下の通り

--
-- Nask.cf
-- $ bnfc -m -cpp_stl nask.cf
--
comment "#" "\\n" ;
comment ";" "\\n" ;

-- terminator Statement "\\n" ;
entrypoints Program;

Prog.           Program        ::= [Statement] ;

(:[]).          [Statement]    ::= Statement ;
(:).            [Statement]    ::= Statement [Statement] ;

LabelStmt.      Statement      ::= Label ;
DeclareStmt.    Statement      ::= Ident "EQU" Exp ;
ConfigStmt.     Statement      ::= "[" ConfigType String "]" ;
MnemonicStmt.   Statement      ::= Opcode [MnemonicArgs] ;
OpcodeStmt.     Statement      ::= Opcode ;

(:[]).          [MnemonicArgs] ::= MnemonicArgs ;
(:).            [MnemonicArgs] ::= MnemonicArgs "," [MnemonicArgs] ;
MnemoArg.       MnemonicArgs   ::= Exp ;

EqExp.                   Exp   ::= Exp "==" Exp ;
NeqExp.                  Exp   ::= Exp "!=" Exp ;
LtExp.                   Exp   ::= Exp "<"  Exp ;
GtExp.                   Exp   ::= Exp ">"  Exp ;
LteExp.                  Exp   ::= Exp "<=" Exp ;
GteExp.                  Exp   ::= Exp ">=" Exp ;
PlusExp.                 Exp   ::= Exp "+" Exp ;
MinusExp.                Exp   ::= Exp "-" Exp ;
MulExp.                  Exp   ::= Exp "*" Exp ;
DivExp.                  Exp   ::= Exp "/" Exp ;
ModExp.                  Exp   ::= Exp "%" Exp ;
IndirectAddrExp.         Exp   ::= "[" Exp "]" ;
DatatypeExp.             Exp   ::= DataType "[" Exp "]" ;
RangeExp.                Exp   ::= DataType Exp ":" Exp ;
LabelExp.                Exp   ::= Label ;
ImmExp.                  Exp   ::= Factor ;

NumberFactor. Factor         ::= Integer ;
HexFactor.    Factor         ::= Hex ;
IdentFactor.  Factor         ::= Ident ;
StringFactor. Factor         ::= String ;

BitsConfig.   ConfigType     ::= "BITS" ;
InstConfig.   ConfigType     ::= "INSTRSET" ;
OptiConfig.   ConfigType     ::= "OPTIMIZE" ;
FormConfig.   ConfigType     ::= "FORMAT" ;
PaddConfig.   ConfigType     ::= "PADDING" ;
PadsConfig.   ConfigType     ::= "PADSET" ;
SectConfig.   ConfigType     ::= "SECTION" ;
AbsoConfig.   ConfigType     ::= "ABSOLUTE" ;
FileConfig.   ConfigType     ::= "FILE" ;

ByteDataType.   DataType     ::= "BYTE" ;
WordDataType.   DataType     ::= "WORD" ;
DwordDataType.  DataType     ::= "DWORD" ;

OpcodesAAA.     Opcode       ::= "AAA" ;
OpcodesAAD.     Opcode       ::= "AAD" ;
OpcodesAAS.     Opcode       ::= "AAS" ;
OpcodesAAM.     Opcode       ::= "AAM" ;
...(以下、定数定義が続く)

token Hex '0' ('x'|'X') (digit | ["abcdef"] | ["ABCDEF"])+ '-'* '$'* ;
token Label (letter | digit | '_')+ ':' ;

確認

1行コメントの字句解析と構文解析

DB 0x01, 0x02 

DB 0x02   # comment

DB 0x03   ; comment

DB "hello"
$ ./testnask test.txt

Parse Successful!

[Abstract Syntax]
(Prog [(MnemonicStmt [OpcodesDB] [(MnemoArg [(ImmExp [(HexFactor "0x01")])]), (MnemoArg [(ImmExp [(HexFactor "0x02")])])]), (MnemonicStmt [OpcodesDB] [(MnemoArg [(ImmExp [(HexFactor "0x02")])])]), (MnemonicStmt [OpcodesDB] [(MnemoArg [(ImmExp [(HexFactor "0x03")])])]), (MnemonicStmt [OpcodesDB] [(MnemoArg [(ImmExp [(StringFactor "hello")])])])])

[Linearized Tree]
DB 0x01, 0x02 DB 0x02 DB 0x03 DB "hello"