我要写的不是
identifier [a-zA-Z0-9]+
为了接受仅由数字完成的字符串?
我写了新的野牛和flex文件,以便明确我的问题。 野牛档案:
%{
#include <stdio.h>
#include <string>
using namespace std;
extern int yylex();
extern void yyerror(char*);
%}
//Symbols
%union
{
double double_val;
char *str_val;
};
%token START
%token STOP
%token BEGIN_NUM
%token END_NUM
%token BEGIN_STRING
%token END_STRING
%token <double_val> NUMBER
%token <str_val> IDENTIFIER
%start MyTest
%%
MyTest:
START Block STOP
;
Block:
/* empty */
| Block BEGIN_STRING IDENTIFIER END_STRING { printf("received string: %s \n", $3); }
| Block BEGIN_NUM NUMBER END_NUM { printf("received number: %f \n", $3); }
;
%%
Flex文件:
%{
#include <string>
#include "test.tab.h"
void yyerror(char*);
int yyparse(void);
%}
blanks [ \t\n]+
identifier [a-zA-Z0-9]+
number [0-9][0-9]*(.[0-9]+)?
%%
{blanks} { /* ignore */ };
"<test>" return(START);
"</test>" return(STOP);
"<string>" return(BEGIN_STRING);
"</string>" return(END_STRING);
"<num>" return(BEGIN_NUM);
"</num>" return(END_NUM);
{number} { yylval.double_val = atof(yytext);
return(NUMBER);
}
{identifier} {
yylval.str_val=strdup(yytext);
return(IDENTIFIER);
}
%%
void yyerror (char* str){ printf (" ERROR : Could not parse! %s\n", str );}
int yywrap (void){ }
int main(int num_args, char** args){
if(num_args != 2) {printf("usage: ./parser filename\n"); exit(0);}
FILE* file = fopen(args[1],"r");
if(file == NULL) {printf("couldn't open %s\n",args[1]); exit(0);}
yyin = file;
yyparse();
fclose(file);
}
当我输入此文件时,一切正常:
<test>
<num>1</num>
<string>eeeeee</string>
<num>2</num>
<string>cccc</string>
<num>3</num>
<num>4</num>
<string>asaa</string>
<string>dsa</string>
</test>
但是如果我用一个只有数字的值改变一个字符串字段:
<string>323</string>
我收到语法错误...
答案 0 :(得分:1)
只有数字的字符串会返回令牌public class AppSettings extends PreferenceActivity {
public static MultiSelectListPreference blocked;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
blocked = new MultiSelectListPreference(this);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefFrag()).commit();
}
public static class PrefFrag extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
MultiSelectListPreference blocked = (MultiSelectListPreference)findPreference("markedApps");
if (blocked == null)
Log.e("NullPointerException", "Blocked is null");
AppSelector.populateAppList();
CharSequence[] appNames = new CharSequence[AppSelector.Data.appNames.size()];
CharSequence[] allApps = new CharSequence[AppSelector.Data.allApps.size()];
int i = 0;
for (String appName : AppSelector.Data.appNames)
appNames[i++] = (CharSequence) appName;
i = 0;
for (String app : AppSelector.Data.allApps)
allApps[i++] = (CharSequence) app;
blocked.setEntries(appNames);
blocked.setEntryValues(allApps);
}
}
}
,因此您可以添加规则:
$(document).on('click', "#backClick", function (e) {
//go back a page
parent.history.back();
});
另外,让你的词法分析者只返回function bestComb(nums, target) {
var combinations = [];
var sums = [];
function loop(idx, comb, sum) {
if(idx >= nums.length || sum + nums[idx] > target) {
combinations.push(comb.slice());
sums.push(sum);
return;
}
for(var i = idx; i < nums.length; i++) {
if(sum + nums[i] > target) break;
if(sum + nums[i] === target) {
combinations.push(comb.slice());
combinations[combinations.length - 1].push(nums[i]);
sums.push(sum + nums[i]);
break;
}
comb.push(nums[i]);
loop(i + 1, comb, sum + nums[i]);
comb.pop();
}
}
nums = nums.slice();
nums.sort(function(a,b) {return a - b});
loop(0, [], 0);
if(sums.length === 0) return null;
var maxSum = sums[0],
maxComb = combinations[0];
for(var i = 1; i < sums.length; i++) {
if(sums[i] > maxSum || sums[i] === maxSum && combinations[i].length < maxComb.length) {
maxSum = sums[i];
maxComb = combinations[i];
}
}
return maxComb;
}
var nums = [25, 60, 13, 48, 23, 29, 27, 22];
var solution = bestComb(nums, 90);
console.log(solution);
NUMBER
以外的所有内容Block: Block BEGIN_STRING NUMBER END_STRING { printf("received number as string: %f \n", $3); }
代币,并在任何地方使用它:
TEXT
此外,你的词法分析者应该有这样的规则:
<
或
>
最后。
如果没有这样的规则,输入中的奇数字符只会被回显到输出,这几乎肯定不是你想要的编译器(尽管对于只是对一些简单的文本处理器进行一些修改可能没问题。输入,并留下其余的作为传递)。您想要的取决于您希望如何处理错误 - 如果您的语法中没有错误恢复规则而只是想忽略额外的字符,那么前者很好,而如果您在语法中实现了错误恢复方案,后者将任何额外的字符作为单个标记提供给解析器,您的错误恢复可以使用它们做更聪明的事情。