main.js 836 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {
  2. InputStream,
  3. CommonTokenStream
  4. } from 'antlr4/index';
  5. import { AnalisadorSintatico } from './asa/analisadorSintatico';
  6. import Lexers from '../grammar/';
  7. const lang = 'pt_br';
  8. const ivprogLexer = Lexers[lang];
  9. const input = `programa {
  10. const real PI
  11. inteiro i
  12. const inteiro a[5][5], b, c[i]
  13. funcao
  14. inteiro teste()
  15. {
  16. inteiro i[5];
  17. }
  18. }`;
  19. const lexer = new ivprogLexer(new InputStream(input));
  20. const stream = new CommonTokenStream(lexer);
  21. stream.fill();
  22. let i = 1;
  23. let token = null;
  24. while ((token = stream.LT(i)).type !== ivprogLexer.EOF && token.type !== ivprogLexer.ESPACO) {
  25. console.log(`${token.type}-${token.text}`);
  26. console.log('\n')
  27. i++;
  28. }
  29. const anaSin = new AnalisadorSintatico(input, ivprogLexer);
  30. try {
  31. console.log(anaSin.parseTree().global);
  32. } catch(a) {
  33. console.log(a);
  34. }