antlr3

Scanner (Lexing keywords with ANTLR)

倾然丶 夕夏残阳落幕 提交于 2021-02-04 21:28:29
问题 I have been working on writing a scanner for my program and most of the tutorials online include a parser along with the scanner. It doesn't seem possible to write a lexer without writing a parser at the same time. I am only trying to generate tokens, not interpret them. I want to recognize INT tokens, float tokens, and some tokens like "begin" and "end" I am confused about how to match keywords. I unsuccessfully tried the following: KEYWORD : KEY1 | KEY2; KEY1 : {input.LT(1).getText().equals

Scanner (Lexing keywords with ANTLR)

守給你的承諾、 提交于 2021-02-04 21:27:49
问题 I have been working on writing a scanner for my program and most of the tutorials online include a parser along with the scanner. It doesn't seem possible to write a lexer without writing a parser at the same time. I am only trying to generate tokens, not interpret them. I want to recognize INT tokens, float tokens, and some tokens like "begin" and "end" I am confused about how to match keywords. I unsuccessfully tried the following: KEYWORD : KEY1 | KEY2; KEY1 : {input.LT(1).getText().equals

ANTLR Nested Functions

六眼飞鱼酱① 提交于 2020-05-13 14:13:22
问题 Is ANTLR right for this project? I'm looking to process and transform a string entered in by a user which may include custom functions. For example, the user might write something like $CAPITALIZE('word') in a string and I want to perform the actual transformation in the background using StringUtils. I would imagine the users will sometimes write nested functions like: $RIGHT_PAD($RIGHT($CAPITALIZE('a123456789'),6),3,'0') Where the expected output would be a string value of 'A12345000'. I

ANTLR Nested Functions

柔情痞子 提交于 2020-05-13 14:05:27
问题 Is ANTLR right for this project? I'm looking to process and transform a string entered in by a user which may include custom functions. For example, the user might write something like $CAPITALIZE('word') in a string and I want to perform the actual transformation in the background using StringUtils. I would imagine the users will sometimes write nested functions like: $RIGHT_PAD($RIGHT($CAPITALIZE('a123456789'),6),3,'0') Where the expected output would be a string value of 'A12345000'. I

LL(1) table-driven compilers with ANTLR or ANTLR3

梦想的初衷 提交于 2020-01-15 10:59:14
问题 Is it possible to create a LL(1) table-driven (non-recursive) compiler with ANTLR or ANTLR3 ? 回答1: No. However since ANTLR is open source you could modify a fork of ANTLR to do it. ANTLR builds lexers and parsers as recursive descent source code. This is why ANTLR is easy to use and popular because people can look at the source code and understand how the lexer and parser work versus looking at table entries. Because it is source code, one can also use tools to debug the source code. If ANTLR

Generate AST in the form of a dot file

≯℡__Kan透↙ 提交于 2020-01-15 04:30:50
问题 I am working with ANTLR4 to generate AST of a java source code and i had to move to ANTLR3 because i was not getting much help and documentation and it was really tough to proceed.I managed to generate AST but not in a visual format. Then i came across an awesome answer and i was really able to generate AST in a DOT file but there was a slight problem. My code: import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.tree.CommonTree;

Generate AST in the form of a dot file

强颜欢笑 提交于 2020-01-15 04:30:26
问题 I am working with ANTLR4 to generate AST of a java source code and i had to move to ANTLR3 because i was not getting much help and documentation and it was really tough to proceed.I managed to generate AST but not in a visual format. Then i came across an awesome answer and i was really able to generate AST in a DOT file but there was a slight problem. My code: import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.ANTLRFileStream; import org.antlr.runtime.tree.CommonTree;

Parsing a templating language

自古美人都是妖i 提交于 2020-01-14 03:59:08
问题 I'm trying to parse a templating language and I'm having trouble correctly parsing the arbitrary html that can appear between tags. So far what I have is below, any suggestions? An example of a valid input would be {foo}{#bar}blah blah blah{zed}{/bar}{>foo2}{#bar2}This Should Be Parsed as a Buffer.{/bar2} And the grammar is: grammar g; options { language=Java; output=AST; ASTLabelType=CommonTree; } /* LEXER RULES */ tokens { } LD : '{'; RD : '}'; LOOP : '#'; END_LOOP: '/'; PARTIAL : '>';

ANTLR lexer can't lookahead at all

老子叫甜甜 提交于 2020-01-11 09:23:10
问题 I have the following grammar: rule: 'aaa' | 'a' 'a'; It can successfully parse the string 'aaa', but it fails to parse 'aa' with the following error: line 1:2 mismatched character '<EOF>' expecting 'a' FYI, it is the lexer's problem not the parser's because I don't even call the parser. The main function looks like: @members { public static void main(String[] args) throws Exception { RecipeLexer lexer = new RecipeLexer(new ANTLRInputStream(System.in)); for (Token t = lexer.nextToken(); t

How to avoid building intermediates and useless AST nodes with ANTLR3?

被刻印的时光 ゝ 提交于 2020-01-11 07:09:15
问题 I wrote an ANTLR3 grammar subdivided into smaller rules to increase readability. For example: messageSequenceChart: 'msc' mscHead bmsc 'endmsc' end ; # Where mscHead is a shortcut to : mscHead: mscName mscParameterDecl? timeOffset? end mscInstInterface? mscGateInterface ; I know the built-in ANTLR AST building feature allows the user to declare intermediate AST nodes that won't be in the final AST. But what if you build the AST by hand? messageSequenceChart returns [msc::MessageSequenceChart*