Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PARSER_BEGIN(Karloff)
- import java.io.*;
- public class Karloff {
- public static void main(String args[]) throws ParseException, IOException {
- // javacc -NOSTATIC Karloff.jj
- // pasta dos trabalhos: https://drive.google.com/drive/folders/1WRvFKckGv7ciZ9fAlITJIOxZzMp0Jcln?usp=sharing
- Karloff parser = new Karloff(new FileReader(args[0]));
- parser.KarloffLexicon();
- parser = new Karloff(new FileReader(args[0]));
- parser.Karloff();
- }
- }
- PARSER_END(Karloff)
- SKIP:{
- " "
- | "\t"
- | "\n"
- | "\r"}
- TOKEN:{
- <MAIN:"main">
- | <ACHAVES:"{">
- | <FCHAVES:"}">
- | <APAREN:"(">
- | <FPAREN:")">
- | <SEMIC:";">
- | <COMMA:",">
- | <FUNC:"func">
- | <TVOID:"void">
- | <NEWVAR:"newVar">
- | <TINT:"int">
- | <TBOOL:"bool">
- | <IF:"if">
- | <THEN:"then">
- | <WHILE:"while">
- | <REPEAT:"repeat">
- | <UNTIL:"until">
- | <RETURN:"return">
- | <TRUE:"true">
- | <FALSE:"false">
- | <OUTPUT:"System.output">
- | <INPUT:"System.input">
- | <PLUS:"+">
- | <MINUS:"-">
- | <ASTERISK:"*">
- | <SLASH:"/">
- | <AMP:"&">
- | <VERBAR:"|">
- | <LESS:"<">
- | <GREATER:">">
- | <ASSIGN:"=">
- | <EQUAL:"==">
- }
- TOKEN:{
- <NUM: (["0"-"9"] | ["1"-"9"] (["0"-"9"])*) ("." (["0"-"9"])* ["1"-"9"])? ("E" (["+","-"])? ["1"-"9"] (["0"-"9"])* )?>
- | <ID: ["a"-"z","A"-"Z"] (("_")?["a"-"z","A"-"Z","0"-"9"])* >
- }
- void Karloff () :
- {Token t;}
- {
- Main() Func() <EOF> {System.out.println("Succesful!");}
- }
- void Main () :
- {}
- {
- <TVOID> <MAIN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES>
- }
- void Vardecl () :
- {}
- {
- (<NEWVAR> Tipo() <ID> <SEMIC>)*
- }
- void Tipo () :
- {}
- {
- <TBOOL> | <TINT>
- }
- void Seqcomandos () :
- {}
- {
- (Comando() <SEMIC>)*
- }
- void Comando () :
- {}
- {
- <ID> (<ASSIGN> Exp() | <APAREN> ( Listaexp() )? <FPAREN>)
- | <IF> <APAREN> Exp() <FPAREN> <THEN> <ACHAVES> Seqcomandos() <FCHAVES>
- | <WHILE> <APAREN> Exp() <FPAREN> <ACHAVES> Seqcomandos() <FCHAVES>
- | <REPEAT> <ACHAVES> Seqcomandos() <FCHAVES> <UNTIL> <APAREN> Exp() <FPAREN>
- | <RETURN> Exp()
- | <OUTPUT> <APAREN> Exp() <FPAREN>
- | <INPUT> <APAREN> Exp() <FPAREN>
- }
- void Exp () :
- {}
- {
- <APAREN> Exp() Op() Exp() <FPAREN>
- | Fator()
- }
- void Fator () :
- {}
- {
- <ID> ( <APAREN> ( Listaexp() )? <FPAREN> )?
- | <NUM>
- | <TRUE>
- | <FALSE>
- }
- void Op () :
- {}
- {
- <PLUS> | <MINUS> | <ASTERISK> | <SLASH> | <AMP>
- | <VERBAR> | <LESS> | <GREATER> | <ASSIGN> | <EQUAL>
- }
- void Listaexp () :
- {}
- {
- Exp() ( <COMMA> Exp() )*
- }
- void Func () :
- {}
- {
- ( <FUNC> Tipo() <ID> <APAREN> (Listaarg())? <FPAREN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES> )*
- }
- void Listaarg () :
- {}
- {
- Tipo() <ID> ( <COMMA> Tipo() <ID> )*
- }
- void KarloffLexicon () :
- {Token t;}
- {
- (
- <MAIN> { System.out.println("Palavra reservada: main");}
- | <ACHAVES> { System.out.println("Abre chaves: {");}
- | <FCHAVES> { System.out.println("Fecha chaves: }");}
- | <APAREN> { System.out.println("Abre parenteses: (");}
- | <FPAREN> { System.out.println("Fecha parenteses: )");}
- | <SEMIC> { System.out.println("Ponto e virgula: ;");}
- | <COMMA> { System.out.println("Virgula: ,");}
- | <FUNC> { System.out.println("Palavra reservada: func");}
- | <TVOID> { System.out.println("Palavra reservada: void");}
- | <NEWVAR> { System.out.println("Palavra reservada: newVar");}
- | <TINT> { System.out.println("Palavra reservada: int");}
- | <IF> { System.out.println("Palavra reservada: if");}
- | <THEN> { System.out.println("Palavra reservada: then");}
- | <WHILE> { System.out.println("Palavra reservada: while");}
- | <REPEAT> { System.out.println("Palavra reservada: repeat");}
- | <UNTIL> { System.out.println("Palavra reservada: until");}
- | <RETURN> { System.out.println("Palavra reservada: return");}
- | <TRUE> { System.out.println("Palavra reservada: true");}
- | <FALSE> { System.out.println("Palavra reservada: false");}
- | <OUTPUT> { System.out.println("Palavra reservada: System.output");}
- | <INPUT> { System.out.println("Palavra reservada: System.input");}
- | <PLUS> { System.out.println("Operador 'mais': +");}
- | <MINUS> { System.out.println("Operador 'menos': -");}
- | <ASTERISK> { System.out.println("Operador 'asterisco': *");}
- | <SLASH> { System.out.println("Operador 'barra': /");}
- | <AMP> { System.out.println("Operador 'e comercial': &");}
- | <VERBAR> { System.out.println("Operador 'pipe': |");}
- | <LESS> { System.out.println("Operador 'menor': <");}
- | <GREATER> { System.out.println("Operador 'maior': >");}
- | <ASSIGN> { System.out.println("Operador 'atribuicao': =");}
- | <EQUAL> { System.out.println("Operador 'igualdade': ==");}
- | t=<NUM> { System.out.println("NĂºmero: "+ t.image);}
- | t=<ID> { System.out.println("Identificador: "+ t.image);}
- )*
- <EOF>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement