% C'est un test

% symbole + : 1 ou plus
%         * : 0 ou plus
% pour absence de saut de ligne -->
% {NUMx} pour numérotation
% {SOURCE}ou {SOURCE-} pour le texte initial

>main

"Aide"
Bienvenue dans l'outil d'aide au codage
Traducteur version 1.0, B. CONRARD 
Grammaire du 14/5/14 modifie pour IMA2A
modifié pour ROUSSEL et SUN

[instruction]:inst
    ; {SOURCE}{inst}

>instruction

[SymboleSi] [blanc]* [Expression]:exp [SymboleAlors] [instruction]:inst
{exp}    TST R16
    BREQ eti{NUM0}
{inst}eti{NUM0}:

[SymboleSi] [blanc]* [Expression]:exp [SymboleSaut] [Etiquette]:eti [blanc]*  [fin]
{exp}    TST R16
    BREQ eti{NUM0}
    JMP {eti}
eti{NUM0}:

[SymboleSi] [blanc]* [Expression]:exp [SymboleAppel] [Etiquette]:eti [blanc]*  [fin]
{exp}    TST R16
    BREQ eti{NUM0}
    CALL {eti}
eti{NUM0}:

[blanc]* [Registre]:regd [blanc]* [SymboleAffectation] [blanc]* [Registre]:regs  [blanc]* [fin]
    MOV {regd},{regs}

[blanc]* [Variable]:var [blanc]* [SymboleAffectation] [blanc]* [Registre]:regs  [blanc]* [fin]
    STS {var},{regs}

[blanc]* [Registre]:regd [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]* [fin]
{exp}    MOV {regd},R16

[blanc]* [VariableIO]:var [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]* [fin]
{exp}    OUT {var},R16

[blanc]* [Variable]:var [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]*  [fin]
{exp}    STS {var},R16

[SymboleSaut] [Etiquette]:eti [blanc]*  [fin]
    JMP {eti}

[SymboleAppel] [Etiquette]:eti [blanc]*  [fin]
    CALL {eti}

>2ndeInst

[blanc]* "," [blanc]* [Registre]:regd [blanc]* [SymboleAffectation] [blanc]* [Registre]:regs [blanc]*
    MOV {regd},{regs}

[blanc]* "," [blanc]* [Variable]:var [blanc]* [SymboleAffectation] [blanc]* [Registre]:regs [blanc]*
    STS {var},{regs}

[blanc]* "," [blanc]* [Registre]:regd [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]*
{exp}    MOV {regd},R16

[blanc]* "," [blanc]* [VariableIO]:var [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]*
{exp}    OUT {var},R16

[blanc]* "," [blanc]* [Variable]:var [blanc]* [SymboleAffectation] [blanc]* [Expression]:exp [blanc]*
{exp}    STS {var},R16

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>fin

";" [toutcar]* [finligne]
-->

[finligne]
-->

>toutcar

[AlphaNum]:c
-->{c}

[blanc]
--> 

'<>[]_{}@&~()-çà+éèù%"$£!:;.,?!':c
-->{c}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Expression

[Expression5]:exp 
-->{exp}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Expression5

[Expression25]:exp
-->{exp}

%%%%%%%%%%%%%%%%%%%%% Operateur OU booleen

>Expression25

[Expression30]:th [OpExpression25]*:calc
-->{th}{calc}

>OpExpression25

[SymboleOuBool] [Expression30]:exp
    TST R16
    BRNE eti{NUM0}
{exp}eti{NUM0}:

%%%%%%%%%%%%%%%%%%%%% Operateur ET booleen

>Expression30

[Expression35]:th [OpExpression30]*:calc
-->{th}{calc}

>OpExpression30

[SymboleEtBool] [Expression35]:exp
    TST R16
    BREQ eti{NUM0}
{exp}eti{NUM0}:

%%%%%%%%%%%%%%%%%%%%% Comparaison

>Expression35

[Expression40]:th [OpExpression35]*:calc
-->{th}{calc}

>OpExpression35

[SymboleCmp]:symb [Expression45]:exp
    PUSH R16
{exp}    POP R17
    CP R17,R16
    {symb} eti{NUM0}
    CLR R16
    RJMP eti{NUM1}
eti{NUM0}:
    LDI R16,0x01
eti{NUM1}:

[SymboleCmpSup] [Expression45]:exp
    PUSH R16
{exp}    POP R17
    CP R17,R16
    BREQ eti{NUM0}
    BRLO eti{NUM0}
    LDI R16,0x01
    RJMP eti{NUM1}
eti{NUM0}:
    CLR R16
eti{NUM1}:

[SymboleCmpInfEgal] [Expression45]:exp
    PUSH R16
{exp}    POP R17
    CP R17,R16
    BREQ eti{NUM0}
    BRLO eti{NUM0}
    CLR R16
    RJMP eti{NUM1}
eti{NUM0}:
    LDI R16,0x01
eti{NUM1}:

>SymboleCmp

[SymboleCmpEgal]
-->BREQ

[SymboleCmpDiff]
-->BRNE

[SymboleCmpInf]
-->BRLO

[SymboleCmpSupEgal]
-->BRSH


%%%%%%%%%%%%%%%%%%%%% OU Logique

>Expression40

[Expression45]:th [OpExpression40]*:calc
-->{th}{calc}

>OpExpression40

[SymboleOuLogique] [Registre]:reg [Expression45]-
    OR R16,{reg}

[SymboleOuLogique] [Valeur]:val [Expression45]-
    ORI R16,{val}

[SymboleOuLogique] [VariableIO]:var [Expression45]-
    IN R17,{var}
    OR R16,R17

[SymboleOuLogique] [Tableau]:var [Expression]:exp [blanc]* "]" [Expression45]-
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    OR R16,R17

[SymboleOuLogique] [Variable]:var [Expression45]-
    LDS R17,{var}
    OR R16,R17

[SymboleOuLogique] [Expression45]:exp
    PUSH R16
{exp}    POP R17
    OR R16,R17

%%%%%%%%%%%%%%%%%%%%% ET Logique

>Expression45

[Expression50]:th [OpExpression45]*:calc
-->{th}{calc}

>OpExpression45

[SymboleEtLogique] [Registre]:reg [OpExpression50]-
    AND R16,{reg}

[SymboleEtLogique] [Valeur]:val [OpExpression50]-
    ANDI R16,{val}

[SymboleEtLogique] [VariableIO]:var [OpExpression50]-
    IN R17,{var}
    AND R16,R17

[SymboleEtLogique] [Tableau]:var [Expression]:exp [blanc]* "]" [OpExpression50]-
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    AND R16,R17

[SymboleEtLogique] [Variable]:var [OpExpression50]-
    LDS R17,{var}
    AND R16,R17

[SymboleEtLogique] [Expression50]:exp
    PUSH R16
{exp}    POP R17
    AND R16,R17

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Expression50

[Expression60]:th [OpExpression50]*:calc
-->{th}{calc}

>OpExpression50

[SymboleAddition] [Registre]:reg [OpExpression60]-
    ADD R16,{reg}

[SymboleAddition] [Valeur]:val [OpExpression60]-
    LDI R17,{val}
    ADD R16,R17

[SymboleAddition] [VariableIO]:var [OpExpression60]-
    IN R17,{var}
    ADD R16,R17

[SymboleAddition] [Tableau]:var [Expression]:exp [blanc]* "]" [OpExpression60]-
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    ADD R16,R17

[SymboleAddition] [Variable]:var [OpExpression60]-
    LDS R17,{var}
    ADD R16,R17

[SymboleAddition] [Expression60]:exp
    PUSH R16
{exp}    POP R17
    ADD R16,R17

[SymboleSoustraction] [Registre]:reg [OpExpression60]-
    SUB R16,{reg}

[SymboleSoustraction] [Valeur]:val [OpExpression60]-
    LDI R17,{val}
    SUB R16,R17

[SymboleSoustraction] [VariableIO]:var [OpExpression60]-
    IN R17,{var}
    SUB R16,R17

[SymboleSoustraction] [Tableau]:var [Expression]:exp [blanc]* "]" [OpExpression60]-
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    SUB R16,R17

[SymboleSoustraction] [Variable]:var [OpExpression60]-
    LDS R17,{var}
    SUB R16,R17

[SymboleSoustraction] [Expression60]:exp
    PUSH R16
{exp}    MOV R17,R16
    POP R16
    SUB R16,R17

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Expression60

[Expression100]:th [OpExpression60]*:prod
-->{th}{prod}

>OpExpression60

[SymboleMultiplier] [Registre]:reg
    MUL R16,{reg}
    MOV R16,R0

[SymboleMultiplier] [Valeur]:val
    LDI R17,{val}
    MUL R16,R17
    MOV R16,R0

[SymboleMultiplier] [VariableIO]:var
    IN R17,{var}
    MUL R16,R17
    MOV R16,R0

[SymboleMultiplier] [Tableau]:var [Expression]:exp [blanc]* "]"
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    MUL R16,R17
    MOV R16,R0

[SymboleMultiplier] [Variable]:var
    LDS R17,{var}
    MUL R16,R17
    MOV R16,R0

[SymboleMultiplier] [Expression100]:exp
    PUSH R16
{exp}    POP R17
    MUL R16,R17
    MOV R16,R0

% diviser

[SymboleDiviser] [Registre]:reg
    MOV R17,{reg}
    SER R18
eti{NUM0}:
    INC R18
    SUB R16,R17
    BRCC eti{NUM0}
    MOV R16,R18

[SymboleDiviser] [Valeur]:val
    LDI R17,{val}
    SER R18
eti{NUM0}:
    INC R18
    SUB R16,R17
    BRCC eti{NUM0}
    MOV R16,R18

[SymboleDiviser] [VariableIO]:var
    IN R17,{var}
    SER R18
eti{NUM0}:
    INC R18
    SUB R16,R17
    BRCC eti{NUM0}
    MOV R16,R18

[SymboleDiviser] [Tableau]:var [Expression]:exp [blanc]* "]"
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
    SER R18
eti{NUM0}:
    INC R18
    SUB R16,R17
    BRCC eti{NUM0}
    MOV R16,R18

[SymboleDiviser] [Variable]:var
    LDS R17,{var}
    SER R18
eti{NUM0}:
    INC R18
    SUB R16,R17
    BRCC eti{NUM0}
    MOV R16,R18

[SymboleDiviser] [Expression100]:exp
    PUSH R16
{exp}    POP R17
    SER R18
eti{NUM0}:
    INC R18
    SUB R17,R16
    BRCC eti{NUM0}
    MOV R16,R18

% Modulo

[SymboleModulo] [Registre]:reg
    MOV R17,{reg}
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}

[SymboleModulo] [Valeur]:val
    LDI R17,{val}
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}

[SymboleModulo] [VariableIO]:var
    IN R17,{var}
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}

[SymboleModulo] [Tableau]:var [Expression]:exp [blanc]* "]"
    PUSH R16
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R17,X
    POP R16
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}

[SymboleModulo] [Variable]:var
    LDS R17,{var}
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}

[SymboleModulo] [Expression100]:exp
    PUSH R16
{exp}    MOV R17,R16
    POP R16
eti{NUM0}:
    SUB R16,R17
    BRCC eti{NUM0}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Expression100

[Terme]:th
-->{th}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Terme

[Registre]:reg
    MOV R16,{reg}

[VariableIO]:var
    IN R16,{var}

[VariableROM]:var
    LDI R30,low({var}<<1)
    LDI R31,high({var}<<1)
    LPM R16,Z

[TableauROM]:var [Expression]:exp [blanc]* "]"
{exp}    LDI R30,low({var}<<1)
    LDI R31,high({var}<<1)
    CLR R17
    ADD R30,R16
    ADC R31,R17
    LPM R16,Z

[Tableau]:var [Expression]:exp [blanc]* "]"
{exp}    LDI R26,low({var})
    LDI R27,high({var})
    CLR R17
    ADD R26,R16
    ADC R27,R17
    LD R16,X

[Variable]:var
    LDS R16,{var}

[Valeur]:val
    LDI R16,{val}


[blanc]* "(" [Expression]:exp [blanc]* ")"
-->{exp}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Variable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>Registre

[blanc]* 'Rr' '0123456789'+:id
-->R{id}

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@REG" "["-
-->{l1}{l2}

>VariableIO

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@IO"
-->{l1}{l2}

>VariableROM

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@ROM" "["-
-->{l1}{l2}

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@pgm" "["-
-->{l1}{l2}

>Variable

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@"-
-->{l1}{l2}

>Tableau

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "["
-->{l1}{l2}

>TableauROM

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@ROM["
-->{l1}{l2}

[blanc]* [Alpha]:l1 [AlphaNum]*:l2 "@pgm["
-->{l1}{l2}

>Valeur

[blanc]* "0b" '01'+:val
-->0b{val}

[blanc]* "0x" '0123456789abcdefABCDEF'+:val
-->0x{val}

[blanc]* '0123456789'+:val
-->{val}

[blanc]* "'" [toutcar]:car "'"
-->'{car}'

>Etiquette

[blanc]* [Alpha]:l1 [AlphaNum]*:l2
-->{l1}{l2}

%%%%%%%%%%%%%%%%%%%%%%%%%%%% SYMBOLE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

>SymboleAppel

[blanc]* "appel"
-->


>SymboleSaut

[blanc]* "saut"
-->

[blanc]* "goto"
-->

[blanc]* "jump"
-->

>SymboleSi

[blanc]* "si"
-->

[blanc]* "if"
-->

[blanc]* "SI"
-->

[blanc]* "IF"
-->

>SymboleAlors

[blanc]* "then"
-->

[blanc]* "alors"
-->

[blanc]* "THEN"
-->

[blanc]* "ALORS"
-->

>SymboleSeparateur

[blanc]* ","
-->

>SymboleEtBool

[blanc]* "&&"
-->

[blanc]* "et"
-->

>SymboleOuBool

[blanc]* "||"
-->

[blanc]* "ou"
-->

>SymboleCmpInf

[blanc]* "<"
-->

>SymboleCmpInfEgal

[blanc]* "<="
-->

>SymboleCmpSup

[blanc]* ">"
-->

>SymboleCmpSupEgal

[blanc]* ">="
-->

>SymboleCmpDiff

[blanc]* "!="
-->

[blanc]* "<>"
-->

>SymboleCmpEgal

[blanc]* "=="
-->

[blanc]* "="
-->

>SymboleEtLogique

[blanc]* "&"
-->

>SymboleOuLogique

[blanc]* "|"
-->

>SymboleDiviser

[blanc]* "/"
-->

>SymboleModulo

[blanc]* "%"
-->

>SymboleMultiplier

[blanc]* "*"
-->

>SymboleOuBool

[blanc]* "ou"
-->

>SymboleAddition

[blanc]* "+"
-->

>SymboleSoustraction

[blanc]* "-"
-->

>SymboleAffectation

[blanc]* "<-"
-->

[blanc]* "="
-->

>Alpha

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_':lettre
-->{lettre}

>AlphaNum

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_':lettre
-->{lettre}

>Num

'0123456789'+:val
-->{val}

>blanc

" "
-->

[tabulation]
-->