- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个程序来将编程语言的非终结符与终结符分开。枚举器跟踪非终结符的标记名称。非终结符 ID 设置为 0,所有其他 ID 均在其后枚举(理应如此)。然而,当程序找到非终结符时,它会打印一个奇怪的值 1953167781,尽管有些结果确实为 0。那些看起来像 1953... 的值的顺序如下:open int num = 7
,num
是非终结符。 num
不被视为 0,而是被视为 1953167781。不过,如果它是这样的声明:guarded class test{}
,则非终结符 test
为 0,而不是 1953167781。这种现象是否有可能的原因?提前感谢您的反馈!下面是输出图片和所发生过程的示例。
编辑
例如,让我们创建 header 来保存我们的声明:
#ifndef __TOK_H__
#define __TOK_H__
#define MAX (1024)
typedef enum {
ID = 0,OPEN,CLOSED,GUARDED,ARTIFICIAL,STATIC,GLOBAL,CONT,SUPER,INT,FLOAT,CHAR,STRING,
BOOL,COLLECTION,CLASS,FUNCT,METHOD,STRUCT,ENUM,IF,ELSE,_OR_,DO,UNTIL,UNLESS,FOR,
FOREACH,IN,TRY,CATCH,EXCEPTION,RETURN,SKIP,BREAK,TERM,NEW,CALL,TRU,FALS,NIL,
INUMBER,FNUMBER,VCHAR,VSTRING,UNDEF,LT,GT,LE,GE,EQ,NE,AND,OR,ADD,ADDE,INCRE,SUB,SUBE,
DECRE,MULT,MULTE,DIV,DIVE,MOD,MODE,EX,EXE,INC,ASI,NOT,INHER,DOT,COM,LP,RP,LB,RB,LBR,
RBR,APO,QUO,SEMCO,EOFT,NAT
} tokentype;
typedef struct {
char str[MAX];
tokentype type;
} token;
void sscan(FILE *);
token generate(FILE *fp);
#endif
这也是:
#ifndef __RESERVED_H__
#define __RESERVED_H__
char *keywords[] = { //40 keywords/reserved words
"open","closed","guarded","artificial","static","global","cont","super",
"int","float","char","string","bool","collection","class","funct","method",
"struct","enum","if","else","or","do","until","unless","for","foreach","in",
"try","catch","exception","return","skip","break","term","new","call","true",
"false","null",
};
char *relative_operators[] = { //8 relops
"<",">","<=",">=","==","!=","&&","||",
};
char operators[6] = { //6 operators
"+-*/%^",
};
char delimeters[11] = { //11 delimeters
".,(){}[];'\"",
};
char unique_operators[] = { //5 unops
"#!&|="
};
#endif
然后是(虽然效率不高)扫描仪和分离器:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reserved.h"
#include "tok.h"
int linenum = 1;
char ch;
token TOKEN;
int isLangPunct(char ch);
int isKEY(char *word);
int isDELIM(char ch);
int isRELOP(char ch);
int isUNIQUE(char ch);
int isOPERA(char ch);
tokentype getKey(char *word);
tokentype getDel(char ch);
void sscan(FILE *fp) {
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
linenum++;
continue;
} else if (isLangPunct(ch) || isalnum(ch) || isspace(ch)) {
continue;
} else {
fprintf(stderr, "Undefined character: %c at line %d.\n", ch, linenum);
}
}
rewind(fp); linenum = 1;
do {
generate(fp);
} while (TOKEN.type != EOFT);
}
int isLangPunct(char ch) {
int r = 0;
if (ch == '#' || ch == '|' || ch == '&' || ch == '=' || ch == '%' ||
ch == '!' || ch == '+' || ch == '-' || ch == '*' || ch == '/' ||
ch == '.' || ch == ',' || ch == '(' || ch == ')' || ch == ';' ||
ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '\'' ||
ch == '"' || ch == '<' || ch == '>' || ch == '^')
{
r = 1;
} return r;
}
token generate(FILE *fp) {
char *word = malloc(sizeof(char) * MAX);
char *number = malloc(sizeof(char) * MAX);
int wi = 0, ni = 0;
while((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
linenum++; continue;
} else if (ch == '\t' || ch == ' ' || ch == '\r') {
continue;
} else if (isalpha(ch)) {
do {
word[wi++] = ch;
} while(isalpha(ch = fgetc(fp)));
word[wi] = '\0';
wi = 0;
strcpy(TOKEN.str, word);
if (isKEY(word)) {
TOKEN.type = getKey(word);
} else {
TOKEN.type = ID;
}
fseek(fp, -1, SEEK_CUR);
printf("%d ", (int)TOKEN.type);
return TOKEN;
} else if (isdigit(ch)) {
do {
number[ni++] = ch;
} while(isdigit(ch = fgetc(fp)));
if (ch == '.') {
do {
number[ni++] = ch;
} while(isdigit(ch = fgetc(fp)));
TOKEN.type = FNUMBER;
} else {
TOKEN.type = INUMBER;
}
number[ni] = '\0';
ni = 0;
strcpy(TOKEN.str, number);
printf("%s ", TOKEN.str);
fseek(fp, -1, SEEK_CUR);
return TOKEN;
} else if (isDELIM(ch)) {
TOKEN.type = getDel(ch);
char *str = &ch;
//working on getting strings and chars to have
//their own value types
strcpy(TOKEN.str, str);
printf("%s ", TOKEN.str);
return TOKEN;
} else if (isRELOP(ch) || isUNIQUE(ch)) {
switch (ch) {
case '<':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = LE;
strcpy(TOKEN.str, "<=");
} else {
TOKEN.type = LT;
strcpy(TOKEN.str, "<");
fseek(fp, -1, SEEK_CUR);
} break;
case '>':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = GE;
strcpy(TOKEN.str, ">=");
} else {
TOKEN.type = GT;
strcpy(TOKEN.str, ">");
fseek(fp, -1, SEEK_CUR);
} break;
case '=':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = EQ;
strcpy(TOKEN.str, "==");
} else {
TOKEN.type = ASI;
strcpy(TOKEN.str, "=");
fseek(fp, -1, SEEK_CUR);
} break;
case '!':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = NE;
strcpy(TOKEN.str, "!=");
} else {
TOKEN.type = NOT;
strcpy(TOKEN.str, "!");
fseek(fp, -1, SEEK_CUR);
} break;
case '&':
if ((ch = fgetc(fp)) == '&') {
TOKEN.type = AND;
strcpy(TOKEN.str, "&&");
} else {
fprintf(stderr, "Token Error: & missing at line %d.\n", linenum);
fseek(fp, -1, SEEK_CUR);
} break;
case '|':
if ((ch = fgetc(fp)) == '|') {
TOKEN.type = OR;
strcpy(TOKEN.str, "||");
} else {
TOKEN.type = INHER;
strcpy(TOKEN.str, "|");
fseek(fp, -1, SEEK_CUR);
} break;
case '#':
TOKEN.type = INC;
strcpy(TOKEN.str, "#");
}
printf("%s ", TOKEN.str);
return TOKEN;
} else if (isOPERA(ch)) {
switch (ch) {
case '+':
if ((ch = fgetc(fp)) == '+') {
TOKEN.type = INCRE;
strcpy(TOKEN.str, "++");
} else if (ch == '=') {
TOKEN.type = ADDE ;
strcpy(TOKEN.str, "+=");
fseek(fp, -1, SEEK_CUR);
} else {
TOKEN.type = ADD ;
strcpy(TOKEN.str, "+");
fseek(fp, -1, SEEK_CUR);
} break;
case '-':
if ((ch = fgetc(fp)) == '-') {
TOKEN.type = DECRE;
strcpy(TOKEN.str, "--");
} else if (ch == '=') {
TOKEN.type = SUBE;
strcpy(TOKEN.str, "-=");
fseek(fp, -1, SEEK_CUR);
} else {
TOKEN.type = SUB;
strcpy(TOKEN.str, "-");
fseek(fp, -1, SEEK_CUR);
} break;
case '*':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = MULTE;
strcpy(TOKEN.str, "*=");
} else {
TOKEN.type = MULT;
strcpy(TOKEN.str, "*");
fseek(fp, -1, SEEK_CUR);
} break;
case '/':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = DIVE;
strcpy(TOKEN.str, "/=");
} else if (ch == '/') {
do {
continue;
} while ((ch = fgetc(fp)) != '\n');
} else {
TOKEN.type = DIV;
strcpy(TOKEN.str, "/");
fseek(fp, -1, SEEK_CUR);
} break;
case '%':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = MODE;
strcpy(TOKEN.str, "%=");
} else {
TOKEN.type = MOD;
strcpy(TOKEN.str, "%");
fseek(fp, -1, SEEK_CUR);
} break;
case '^':
if ((ch = fgetc(fp)) == '=') {
TOKEN.type = EXE;
strcpy(TOKEN.str, "^=");
} else {
TOKEN.type = EX;
strcpy(TOKEN.str, "^");
fseek(fp, -1, SEEK_CUR);
} break;
}
printf("%s ", TOKEN.str);
return TOKEN;
}
}
free(word);
free(number);
TOKEN.type = EOFT;
strcpy(TOKEN.str, "\0");
printf("%s ", TOKEN.str);
return TOKEN;
}
int isKEY(char *word) {
int r = 0;
for (int i = 0; i < 40; i++) {
if (*word == *keywords[i]) {
r = 1;
}
}
return r;
}
int isDELIM(char ch) {
int r = 0;
for (int i = 0; i < 11; i++) {
if (ch == delimeters[i]) {
r = 1;
}
}
return r;
}
int isRELOP(char ch) {
int r = 0;
if (ch == '<' || ch == '>') {
r = 1;
}
return r;
}
int isUNIQUE(char ch) {
int r = 0;
for (int i = 0; i < 5; i++) {
if (ch == unique_operators[i]) {
r = 1;
}
}
return r;
}
int isOPERA(char ch) {
int r = 0;
for (int i = 0; i < 6; i++) {
if (ch == operators[i]) {
r = 1;
}
}
return r;
}
tokentype getKey(char *word) {
tokentype type;
if (!strcmp(word, "open")) { type = OPEN; }
else if (!strcmp(word, "closed")) { type = CLOSED; }
else if (!strcmp(word, "guarded")) { type = GUARDED; }
else if (!strcmp(word, "artificial")) { type = ARTIFICIAL; }
else if (!strcmp(word, "static")) { type = STATIC; }
else if (!strcmp(word, "global")) { type = GLOBAL; }
else if (!strcmp(word, "cont")) { type = CONT; }
else if (!strcmp(word, "super")) { type = SUPER; }
else if (!strcmp(word, "int")) { type = INT; }
else if (!strcmp(word, "float")) { type = FLOAT; }
else if (!strcmp(word, "char")) { type = CHAR; }
else if (!strcmp(word, "string")) { type = STRING; }
else if (!strcmp(word, "bool")) { type = BOOL; }
else if (!strcmp(word, "collection")) { type = COLLECTION; }
else if (!strcmp(word, "class")) { type = CLASS; }
else if (!strcmp(word, "funct")) { type = FUNCT; }
else if (!strcmp(word, "method")) { type = METHOD; }
else if (!strcmp(word, "struct")) { type = STRUCT; }
else if (!strcmp(word, "enum")) { type = ENUM; }
else if (!strcmp(word, "if")) { type = IF; }
else if (!strcmp(word, "else")) { type = ELSE; }
else if (!strcmp(word, "or")) { type = _OR_; }
else if (!strcmp(word, "do")) { type = DO; }
else if (!strcmp(word, "until")) { type = UNTIL; }
else if (!strcmp(word, "unless")) { type = UNLESS; }
else if (!strcmp(word, "for")) { type = FOR; }
else if (!strcmp(word, "foreach")) { type = FOREACH; }
else if (!strcmp(word, "in")) { type = IN; }
else if (!strcmp(word, "try")) { type = TRY; }
else if (!strcmp(word, "catch")) { type = CATCH; }
else if (!strcmp(word, "exception")) { type = EXCEPTION; }
else if (!strcmp(word, "return")) { type = RETURN; }
else if (!strcmp(word, "skip")) { type = SKIP; }
else if (!strcmp(word, "break")) { type = BREAK; }
else if (!strcmp(word, "term")) { type = TERM; }
else if (!strcmp(word, "new")) { type = NEW; }
else if (!strcmp(word, "call")) { type = CALL; }
else if (!strcmp(word, "true")) { type = TRU; }
else if (!strcmp(word, "false")) { type = FALS; }
else if (!strcmp(word, "null")) { type = NIL; }
return type;
}
tokentype getDel(char ch) {
tokentype type;
if (ch == '.') { type = DOT; }
if (ch == ',') { type = COM; }
if (ch == '(') { type = LP; }
if (ch == ')') { type = RP; }
if (ch == '{') { type = LB; }
if (ch == '}') { type = RB; }
if (ch == '[') { type = LBR; }
if (ch == ']') { type = RBR; }
if (ch == '"') { type = QUO; }
if (ch == '\'') { type = APO; }
if (ch == ';') { type = SEMCO; }
return type;
}
在与上述相同的文件中,main()
能够打开文件并读取它,将文件流发送到 sscan(FILE *)
。最后一段是一个示例文件:
# call Sys;
# call SysIO;
static int num = 7;
cont global float fl = 12.895;
cont char letter = 'h';
collection Program {
cont closed string aswkey = "abdcbabcdabdbcdab";
open class quiz {
bool decision = true;
artificial method.study (int time) {};
open quiz() {
//do something
};
};
class test | quiz {
bool descision;
super method.study (int time) {
if (decision == true) {
//do something
} or if (time == 0) {
//do something
} else {
//do nothing
};
time = 50;
};
guarded test (bool n) {
descision = n;
};
};
funct.Enter () {
quiz Quiz = new quiz();
test Test = new test(false);
Test.study(60);
if (something != this) {
//do something
};
term;
};
};
最佳答案
为了了解出了什么问题,让我们看看发生了什么。在 getKey(word)
函数,当一个单词被发送到那里与关键字进行比较时,它将返回 tokentype type
到TOKEN.type
声明。但是,如果该单词与任何比较都不匹配,那么它仍然只返回一次作为 type
的变量。我的电脑上的地址是 1953167781。因此,通过向 getKey(word)
添加最后的 else 语句其中设置 tokentype type
到 ID,它将所有非终结符与 ID 匹配。然而,这并不能解释为什么像 Sys
这样的词和SysIO
也没有变成1953167781。而且这有点令人困惑if(isKEY(word))
无法返回 false,它似乎总是返回 true。尽管它必须返回 false 才能转到 else 语句,该语句将有效地将所有非终结符设置为 ID。但这仍然提出了一个我们不会讨论但应该思考的问题:为什么像 Sys
这样的非终结符会出现?和SysIO
成为 ID 的(请注意 getKey(word)
函数没有新的最终 else 语句)在始终为 true if 语句内,当它必须返回 false 才能发生这种情况时,为什么其他类似的单词没有相同的采取的类(class)? 不管怎样,它运行良好,我将把它留给专家来弄清楚为什么它会做上一个声明中所做的事情。
关于C 枚举不返回未指定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754832/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!