Advertisement
KDOXG

Lexicon

Aug 22nd, 2022 (edited)
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. PARSER_BEGIN(Karloff)
  2.  
  3. import java.io.*;
  4.  
  5. public class Karloff {
  6.  
  7.     public static void main(String args[]) throws ParseException, IOException {
  8.  
  9.         Karloff analisador = new Karloff(new FileInputStream(args[0]));
  10.         analisador.Karloff();
  11.     }
  12.  
  13. }
  14.  
  15. PARSER_END(Karloff)
  16.  
  17. SKIP:{
  18.   " "
  19. | "\t"
  20. | "\n"
  21. | "\r"}
  22.  
  23. TOKEN:{
  24.   <MAIN:"main">
  25. | <ACHAVES:"{">
  26. | <FCHAVES:"}">
  27. | <APAREN:"(">
  28. | <FPAREN:")">
  29. | <SEMIC:";">
  30. | <COMMA:",">
  31. | <FUNC:"func">
  32. | <TVOID:"void">
  33. | <NEWVAR:"newVar">
  34. | <TINT:"int">
  35. | <TBOOL:"bool">
  36. | <IF:"if">
  37. | <THEN:"then">
  38. | <WHILE:"while">
  39. | <REPEAT:"repeat">
  40. | <UNTIL:"until">
  41. | <RETURN:"return">
  42. | <TRUE:"true">
  43. | <FALSE:"false">
  44. | <OUTPUT:"System.output">
  45. | <INPUT:"System.input">
  46. | <PLUS:"+">
  47. | <MINUS:"-">
  48. | <ASTERISK:"*">
  49. | <SLASH:"/">
  50. | <AMP:"&">
  51. | <VERBAR:"|">
  52. | <LESS:"<">
  53. | <GREATER:">">
  54. | <ASSIGN:"=">
  55. | <EQUAL:"==">
  56.  
  57. }
  58.  
  59. TOKEN:{
  60.   <NUM: (["0"-"9"] | ["1"-"9"] (["0"-"9"])*) ("." (["0"-"9"])* ["1"-"9"])? ("E" (["+","-"])? ["1"-"9"] (["0"-"9"])* )?>
  61. | <ID: ["a"-"z","A"-"Z"] (("_")?["a"-"z","A"-"Z","0"-"9"])* >
  62.  
  63. }
  64.  
  65. void Karloff () :
  66. {Token t;}
  67. {
  68.  
  69. (
  70.   <MAIN> { System.out.println("Palavra reservada: main");}
  71. | <ACHAVES> { System.out.println("Abre chaves: {");}
  72. | <FCHAVES> { System.out.println("Fecha chaves: }");}
  73. | <APAREN> { System.out.println("Abre parenteses: (");}
  74. | <FPAREN> { System.out.println("Fecha parenteses: )");}
  75. | <SEMIC> { System.out.println("Ponto e virgula: ;");}
  76. | <COMMA> { System.out.println("Virgula: ,");}
  77. | <FUNC> { System.out.println("Palavra reservada: func");}
  78. | <TVOID> { System.out.println("Palavra reservada: void");}
  79. | <NEWVAR> { System.out.println("Palavra reservada: newVar");}
  80. | <TINT> { System.out.println("Palavra reservada: int");}
  81. | <TBOOL> { System.out.println("Palavra reservada: int");}
  82. | <IF> { System.out.println("Palavra reservada: if");}
  83. | <THEN> { System.out.println("Palavra reservada: then");}
  84. | <WHILE> { System.out.println("Palavra reservada: while");}
  85. | <REPEAT> { System.out.println("Palavra reservada: repeat");}
  86. | <UNTIL> { System.out.println("Palavra reservada: until");}
  87. | <RETURN> { System.out.println("Palavra reservada: return");}
  88. | <TRUE> { System.out.println("Palavra reservada: true");}
  89. | <FALSE> { System.out.println("Palavra reservada: false");}
  90. | <OUTPUT> { System.out.println("Palavra reservada: System.output");}
  91. | <INPUT> { System.out.println("Palavra reservada: System.input");}
  92. | <PLUS> { System.out.println("Operador 'mais': +");}
  93. | <MINUS> { System.out.println("Operador 'menos': -");}
  94. | <ASTERISK> { System.out.println("Operador 'asterisco': *");}
  95. | <SLASH> { System.out.println("Operador 'barra': /");}
  96. | <AMP> { System.out.println("Operador 'e comercial': &");}
  97. | <VERBAR> { System.out.println("Operador 'pipe': |");}
  98. | <LESS> { System.out.println("Operador 'menor': <");}
  99. | <GREATER> { System.out.println("Operador 'maior': >");}
  100. | <ASSIGN> { System.out.println("Operador 'atribuicao': =");}
  101. | <EQUAL> { System.out.println("Operador 'igualdade': ==");}
  102. | t=<NUM> { System.out.println("NĂºmero: "+ t.image);}
  103. | t=<ID> { System.out.println("Identificador: "+ t.image);}
  104. )*
  105. <EOF>
  106.  
  107. }
  108.  
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement