objective-c - ParseKit - 如何正确处理预处理器语句?

标签 objective-c parsing grammar parsekit

我为 ParseKit 编写了一个 C 语法,它工作得很好,但让我发疯的是预处理器语句。预处理器语句的正确符号定义是什么?

这是我尝试过的简短示例......

@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';
@commentState.fallbackState = delimitState;
@delimitState.fallbackState = symbolState;

@start = Empty | comments | preprocessor;

comments = comment*;
comment = Comment;

@symbols = '#include';

preprocessor = preprocessorIncludes;

preprocessorIncludes = preprocessorIncludeStatement*;
preprocessorIncludeStatement = preprocessorInclude quotedFileName*;

preprocessorInclude = '#include';
quotedFileName = QuotedString;

...但是它不起作用。将其作为简化的语法示例来捕获注释并包含带引号的语句(而不是 < >)。我在这个简单的文件上尝试了这个语法......

/*
 * Cryptographic API.
 *
 * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
 *
 * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
 *
 * Copyright (c) 2008 Adrian-Ken Rueegsegger <ken@codelabs.ch>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

// Here's one line comment

/* One line multiline comment */

#include "ripemd.h"

/* 2nd one line multiline comment */

... 并以/* 一行多行注释 */结束,将其报告为注释标记,然后默默地失败。

所以我尝试将“#include”符号分隔为...

@symbolState = '#' '#';
@symbol = '#';
numSymbol = '#';

preprocessorInclude = numSymbol 'include';

...但仍然无济于事。

也许 Todd 可以提供帮助,但处理“#include”等“符号”的正确方法是什么?

最佳答案

ParseKit 的开发者|在这里。

罗伯特,你的语法非常接近,但我发现你使用嵌套的 *(零个或多个)修饰符导致语法失败。

我认为问题在于您的 @start 语法生成已经将 Empty 作为顶级选项(|ed 与其他两个产品),但是 commentspreprocessor 的子产品都包含带有 *(零或多个)修饰符的产品。那些 * 实际上应该是 + (一个或多个)修饰符,因为您已经考虑了顶级 Empty 的零情况>.

我不完全确定,但我认为这不是 ParseKit 独有的问题,而是我怀疑语法有问题,并且任何此类语法工具包都可能出现过此问题。 (可能是错的)

考虑到这一点,我对语法进行了一些小的调整。这是经过小调整的编辑语法:

@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';
@commentState.fallbackState = delimitState;
@delimitState.fallbackState = symbolState;

@start = (comments | preprocessor)*;

comments = comment+;
comment = Comment;

@symbols = '#include';

preprocessor = preprocessorIncludes;

preprocessorIncludes = preprocessorIncludeStatement+;
preprocessorIncludeStatement = preprocessorInclude quotedFileName;

preprocessorInclude = '#include';
quotedFileName = QuotedString;

请注意我用 * 替换了顶层的 Empty。我用 + 交换了嵌套的 *

使用这个编辑过的语法,我得到了想要的输出(为清楚起见略微截断):

[/*
 * Cryptographic API.
...
 */, // Here's one line comment, /* One line multiline comment */, #include, "ripemd.h", /* 2nd one line multiline comment */]/*
 * Cryptographic API.
...
 *//// Here's one line comment//* One line multiline comment *//#include/"ripemd.h"//* 2nd one line multiline comment */^

此外,为了找到问题,我重写了语法以使其更简单。这样更容易找到问题。然后我将我发现的重新应用到你原来的语法中。如果您有兴趣,这是我想出的简化语法。这就是我在脑海中对这个特定语法的看法:

@reportsCommentTokens = YES;
@commentState = '/';
@singleLineComments = '//';
@multiLineComments = '/*' '*/';

@start = (comment | macro)*;

comment = Comment;

macro = include; // to support other macros, add: ` | define | ifdef` etc.

include = '#' 'include' QuotedString;

关于objective-c - ParseKit - 如何正确处理预处理器语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502766/

相关文章:

ios - 无法从公共(public)可变数组 (Objective-C) 中删除对象

objective-c - NSDictionary objectForKey 返回值

php - 将简码解析为数组

syntax - Modelica 评论

c# - 使用 Irony 解析时包含空格

java - 如何在 Java 语言解析器中正确标记 '@' 符号?

ios - 当我单击 uitableview 单元格时,该单元格的文本变得更粗?

ios - 加密8字节字符串/base64编码/最大长度20字节

c - 防止 getdate_r 填写它不知道的数据

java - 快速可靠地查明源代码文件是否实现接口(interface)的方法