3

我为 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';

......但它仍然没有帮助。

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

4

1 回答 1

2

ParseKit的开发者在这里。

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

我认为问题在于您的@start语法产生式已经Empty作为顶级选项(|ed 与其他两个产生式一起使用),但是随后的子产生式commentspreprocessor都包含带有*(零个或多个)修饰符的产生式。那些*s 实际上应该是+(一个或多个)修饰符,因为您已经考虑了顶级 的零情况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将顶层中的替换为*. 我用 s 交换嵌套*+s。

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

[/*
 * 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;
于 2012-07-16T18:19:31.000 回答