Bison 输入分析器 - 关于可选语法和输入解释的基本问题

发布于 2024-09-02 20:17:25 字数 984 浏览 2 评论 0原文

我对 Flex/Bison 很陌生,所以这是一个非常幼稚的问题。
如果是的话请原谅我。可能看起来像家庭作业问题 - 但我需要根据以下概念实施项目。

我的问题涉及两个部分,
问题1
在 Bison 解析器中,如何提供可选输入的规则。

就像,我需要解析该语句
示例:

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana'

这里比率标记可以是可选的。同样,如果我有许多可选标记,那么如何在解析器中提供相同的语法?

我的代码看起来像,

%启动程序
程序:TK_COUNTRY TK_IDENTIFIER TK_STATE TK_IDENTIFIER TK_POPULATION TK_IDENTIFIER ...
其中所有标记都在词法分析器中定义。由于有很多标记是可选的,如果我使用“|”那么就会有很多不同方式的输入组合成为可能。

问题2
评论很可能包含引号作为输入的一部分,因此我添加了一个用户可以提供的 token 标签来解释相同的内容,

示例:

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana$'s population' -tag=$  

现在,我需要将 Indiana$ 重新解释为 Indiana 自 -tag=$ 以来的版本。

请提供任何输入或相关材料以理解这些主题。

I am very new to Flex/Bison, So it is very navie question.
Pardon me if so. May look like homework question - but I need to implement project based on below concept.

My question is related to two parts,
Question 1
In Bison parser, How do I provide rules for optional input.

Like, I need to parse the statment
Example :

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana'

Here the ratio token can be optional. Similarly, If I have many tokens optional, then How do I provide the grammar in the parser for the same?

My code looks like,

%start program
program : TK_COUNTRY TK_IDENTIFIER TK_STATE TK_IDENTIFIER TK_POPULATION TK_IDENTIFIER ...
where all the tokens are defined in the lexer. Since there are many tokens which are optional, If I use "|" then there will be many different ways of input combination possible.

Question 2
There are good chance that the comment might have quotes as part of the input, so I have added a token -tag which user can provide to interpret the same,

Example :

-country='USA' -state='INDIANA' -population='100' -ratio='0.5' -comment='Census study for Indiana

Now, I need to reinterpret Indiana$'s as Indiana's since -tag=$.

Please provide any input or related material for to understand these topic.

s population' -tag=$

Now, I need to reinterpret Indiana$'s as Indiana's since -tag=$.

Please provide any input or related material for to understand these topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2024-09-09 20:17:25

Q1:我假设我们有 4 个可能的标记: NAME 、 '-'、'=' 和 VALUE

然后语法可能如下所示:

attrs: 
    attr attrs
    | attr
    ;

attr:
   '-' NAME '=' VALUE
   ;

请注意,与您使用特定属性名称区分标记不同,无法说“我们必须有国家、州和人口,但比例是可选的。”
这将是分析解析器生成的数据的程序部分的任务。

Q2:我的理解是,您考虑在解析器运行时改变词法分析的工作方式。这不是一个好主意,至少对于初学者来说不是。您是否已经开始考虑词法分析而不是解析?

Q1: I am assuming we have 4 possible tokens: NAME , '-', '=' and VALUE

Then the grammar could look like this:

attrs: 
    attr attrs
    | attr
    ;

attr:
   '-' NAME '=' VALUE
   ;

Note that, unlike you make specific attribute names distinguished tokens, there is no way to say "We must have country, state and population, but ratio is optional."
This would be the task of that part of the program that analyses the data produced by the parser.

Q2: I understand this so, that you think of changing the way lexical analysis works while the parser is running. This is not a good idea, at least not for a beginner. Have you even started to think about lexical analysis, as opposed to parsing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文