antlr

variation in code generation, using ANTLR4 parse tree visitors

扶醉桌前 提交于 2021-02-16 14:56:12
问题 I am writing transpiler ( myLang -> JS) using ANTLR (javascript target with visitor). Focus is on target code generation part, from the parse tree. As in, how to deal with language source codes variations. To make question clearer, consider two variations below - source#1: PRINT 'hello there' source#2: varGreeting = 'hey!' PRINT varGreeting In case 1, I deal with string. While in case 2, it's a variable. JS target code then needs to be different (below). case 1 with quotes, case 2 without.

Using of nonterminals with same name in rule

大憨熊 提交于 2021-02-11 13:34:28
问题 How can I use as reciever two nonterminals with same name? For example, a have this rule: expression returns [int value] : 'min' factor factor { $expression.value = min($factor1.value, $factor2.value) //here } I need to get value from both factors, but they names are the same. So, if I write $factor.value twice, I get value of first factor. 回答1: I think that it is solution for your problem: expression returns [int value] : 'min' f1=factor f2=factor { $expression.value = min($f1.value, $f2

Using of nonterminals with same name in rule

眉间皱痕 提交于 2021-02-11 13:33:58
问题 How can I use as reciever two nonterminals with same name? For example, a have this rule: expression returns [int value] : 'min' factor factor { $expression.value = min($factor1.value, $factor2.value) //here } I need to get value from both factors, but they names are the same. So, if I write $factor.value twice, I get value of first factor. 回答1: I think that it is solution for your problem: expression returns [int value] : 'min' f1=factor f2=factor { $expression.value = min($f1.value, $f2

ANTLR4 skips empty line only

冷暖自知 提交于 2021-02-08 03:33:17
问题 I am using antlr4 parsing a text file and I am new to it. Here is the part of the file: abcdef //emptyline abcdef In file stream string it will be looked like this: abcdef\r\n\r\nabcdef\r\n In terms of ANTLR4, it offers the "skip" method to skip something like white-space, TAB, and new line symbol by regular expression while parsing. i.e. WS : [\t\s\r\n]+ -> skip ; // skip spaces, tabs, newlines My problem is that I want to skip the empty line only. I don't want to skip every single "\r\n".

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

滴滴 Flink-1.10 升级之路

霸气de小男生 提交于 2021-02-03 11:02:07
简介: 滴滴实时计算引擎从 Flink-1.4 无缝升级到 Flink-1.10 版本,做到了完全对用户透明。并且在新版本的指标、调度、SQL 引擎等进行了一些优化,在性能和易用性上相较旧版本都有很大提升。 一、 背景 在本次升级之前,我们使用的主要版本为 Flink-1.4.2,并且在社区版本上进行了一些增强,提供了 StreamSQL 和低阶 API 两种服务形式。现有集群规模达到了 1500 台物理机,运行任务数超过 12000 ,日均处理数据 3 万亿条左右。 不过随着社区的发展,尤其是 Blink 合入 master 后有很多功能和架构上的升级,我们希望能通过版本升级提供更好的流计算服务。今年 2 月份,里程碑版本 Flink-1.10 发布,我们开始在新版上上进行开发工作,踏上了充满挑战的升级之路。 二、 Flink-1.10 新特性 作为 Flink 社区至今为止的最大的一次版本升级,加入的新特性解决了之前遇到很多的痛点。 1. 原生 DDL 语法与 Catalog 支持 Flink SQL 原生支持了 DDL 语法,比如 CREATE TABLE/CREATE FUNCTION,可以使用 SQL 进行元数据的注册,而不需要使用代码的方式。 也提供了 Catalog 的支持,默认使用 InMemoryCatalog 将信息临时保存在内存中,同时也提供了

ANTLR island grammars and a non-greedy rule that consumes too much

懵懂的女人 提交于 2021-01-29 05:20:30
问题 I'm having a problem with an island grammar and a non-greedy rule used to consume "everything except what I want". Desired outcome: My input file is a C header file, containing function declarations along with typedefs, structs, comments, and preprocessor definitions. My desired output is parsing and subsequent transformation of function declarations ONLY. I would like to ignore everything else. Setup and what I've tried: The header file I'm attempting to lex and parse is very uniform and

Can left recursion be eliminated in all cases in ANTLR?

女生的网名这么多〃 提交于 2021-01-28 22:07:14
问题 say I have the following Grammar #1 expr: expr AND expr | expr OR expr | primary ; and is turned into this. Grammar #2 expr: andExpr | primary ; andExpr: orExpr AND orExpr; orExpr: ... OR ...; but I still don't see how this would solve the problem? In Grammar #1 I can express true and false and true and true or false true or false and true I can keep chaining like this with Grammar #1. But I am not seeing how to achieve this using grammar #2? 回答1: You could write it like this: grammar Test;

How to test ANTLR translation without adding EOF to every rule

為{幸葍}努か 提交于 2021-01-28 05:52:12
问题 I am in the middle of re-writing my translator and I am being much more disciplined about tests this time, since this version is likely to live for more than a few weeks. Because you can run a visitor starting at any node, you can almost write beautiful small tests like this ... expect(parse("some test code", "startGrammarRule")).toEqual(new ASTForGrammarRule()) and then write one ( or a few of these ) for each visitor function EXCEPT that the rule you are invoking is a sub rule, and so does