expecting IDENT错误解决办法

本文分析了一条Hibernate HQL查询语句中出现的语法错误,具体表现为使用了不正确的选择字段方式。通过调整HQL语句,将'a.*'替换为'a',成功解决了编译错误,并介绍了正确的HQL查询格式。

今天的开发爆出如下错误。

org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '*' near line 1, column 10 [select a.* from com.jyzq.hr.bean.Archive a,com.jyzq.hr.bean.Employee e,com.jyzq.hr.bean.Department d where a.contractEndDate = :date and a.status = 'A' and a.empId = e.id and e.status = 'A' and e.type = 'A' and e.deptid=d.id and d.type='A']
 at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
 at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
 at org.hi

 

原因是hql语句报错。

hql:select a.* from Archive a,Employee e,Department d where a.contractEndDate = :date and a.status = 'A' and a.empId = e.id and e.status = 'A' and e.type = 'A'

 

 

把hql改为如下形式,则编译通过。一切正常

select a from Archive a,Employee e,Department d where a.contractEndDate = :date and a.status = 'A' and a.empId = e.id and e.status = 'A' and e.type = 'A'

 

select 后面为对象 a .而非 a.*

在下面代码中加入PL0语言输入输出扩展 这是.h文件 #include <stdio.h> #define norw 11 // no. of reserved words #define txmax 100 // length of identifier table #define nmax 14 // max. no. of digits in numbers #define al 10 // length of identifiers #define amax 2047 // maximum address #define levmax 3 // maximum depth of block nesting #define cxmax 2000 // size of code array #define nul 0x1 #define ident 0x2 #define number 0x4 #define plus 0x8 #define minus 0x10 #define times 0x20 #define slash 0x40 #define oddsym 0x80 #define eql 0x100 #define neq 0x200 #define lss 0x400 #define leq 0x800 #define gtr 0x1000 #define geq 0x2000 #define lparen 0x4000 #define rparen 0x8000 #define comma 0x10000 #define semicolon 0x20000 #define period 0x40000 #define becomes 0x80000 #define beginsym 0x100000 #define endsym 0x200000 #define ifsym 0x400000 #define thensym 0x800000 #define whilesym 0x1000000 #define dosym 0x2000000 #define callsym 0x4000000 #define constsym 0x8000000 #define varsym 0x10000000 #define procsym 0x20000000 enum object { constant, variable, proc }; enum fct { lit, opr, lod, sto, cal, Int, jmp, jpc // functions }; /* lit 0, a : load constant a opr 0, a : execute operation a lod l, a : load variable l, a sto l, a : store variable l, a cal l, a : call procedure a at level l Int 0, a : increment t-register by a jmp 0, a : jump to a jpc 0, a : jump conditional to a */ typedef struct { enum fct f; // function code long l; // level long a; // displacement address } instruction; char* err_msg[] = { /* 0 */ "", /* 1 */ "Found ':=' when expecting '='.", /* 2 */ "There must be a number to follow '='.", /* 3 */ "There must be an '=' to follow the identifier.", /* 4 */ "There must be an identifier to follow 'const', 'var', or 'procedure'.", /* 5 */ "Missing ',' or ';'.", /* 6 */ "Incorrect procedure name.", /* 7 */ "Statement expected.", /* 8 */ "Follow the statement is an incorrect symbol.", /* 9 */ "'.' expected.", /* 10 */ "';' expected.", /* 11 */ "Undeclared identifier.", /* 12 */ "Illegal assignment.", /* 13 */ "':=' expected.", /* 14 */ "There must be an identifier to follow the 'call'.", /* 15 */ "A constant or variable can not be called.", /* 16 */ "'then' expected.", /* 17 */ "';' or 'end' expected.", /* 18 */ "'do' expected.", /* 19 */ "Incorrect symbol.", /* 20 */ "Relative operators expected.", /* 21 */ "Procedure identifier can not be in an expression.", /* 22 */ "Missing ')'.", /* 23 */ "The symbol can not be followed by a factor.", /* 24 */ "The symbol can not be as the beginning of an expression.", /* 25 */ "", /* 26 */ "", /* 27 */ "", /* 28 */ "", /* 29 */ "", /* 30 */ "", /* 31 */ "The number is too great.", /* 32 */ "There are too many levels." }; char ch; // last character read unsigned long sym; // last symbol read char id[al+1]; // last identifier read long num; // last number read long cc; // character count long ll; // line length long kk, err; long cx; // code allocation index char line[81]; char a[al+1]; instruction code[cxmax+1]; char word[norw][al+1]; unsigned long wsym[norw]; unsigned long ssym[256]; char mnemonic[8][3+1]; unsigned long declbegsys, statbegsys, facbegsys; struct { char name[al+1]; enum object kind; long val; long level; long addr; }table[txmax+1]; char infilename[80]; FILE* infile; // the following variables for block long dx; // data allocation index long lev; // current depth of block nesting long tx; // current table index // the following array space for interpreter #define stacksize 50000 long s[stacksize]; // datastore 这是.c文件 // pl/0 compiler with code generation #include <stdlib.h> #include <string.h> #include "pl0.h" void error(long n) { long i; printf("Error=>"); for (i = 1; i <= cc-1; i++) { printf(" "); } printf("|%s(%d)\n", err_msg[n], n); err++; } void getch() { if(cc == ll) { if(feof(infile)) { printf("************************************\n"); printf(" program incomplete\n"); printf("************************************\n"); exit(1); } ll = 0; cc = 0; printf("%5d ", cx); while((!feof(infile)) && ((ch=getc(infile))!='\n')) { printf("%c", ch); ll = ll + 1; line[ll] = ch; } printf("\n"); ll = ll + 1; line[ll] = ' '; } cc = cc + 1; ch = line[cc]; } void getsym() { long i, j, k; while(ch == ' ' || ch == '\t') { getch(); } if(isalpha(ch)) // identified or reserved { k = 0; do { if(k < al) { a[k] = ch; k = k + 1; } getch(); } while(isalpha(ch) || isdigit(ch)); if(k >= kk) { kk = k; } else { do { kk = kk-1; a[kk] = ' '; } while(k < kk); } strcpy(id, a); i = 0; j = norw - 1; do { k = (i+j)/2; if(strcmp(id, word[k]) <= 0) { j = k - 1; } if(strcmp(id, word[k]) >=0) { i = k + 1; } } while(i <= j); if(i-1 > j) { sym = wsym[k]; } else { sym = ident; } } else if(isdigit(ch)) // number { k = 0; num = 0; sym = number; do { num = num * 10 + (ch - '0'); k = k + 1; getch(); } while(isdigit(ch)); if(k > nmax) { error(31); } } else if(ch == ':') { getch(); if(ch == '=') { sym = becomes; getch(); } else { sym = nul; } } else if(ch == '<') { getch(); if(ch == '=') { sym = leq; getch(); } else if(ch == '>') { sym=neq; getch(); } else { sym = lss; } } else if(ch == '>') { getch(); if(ch == '=') { sym=geq; getch(); } else { sym=gtr; } } else { sym = ssym[(unsigned char)ch]; getch(); } } void gen(enum fct x, long y, long z) { if(cx > cxmax) { printf("program too long\n"); exit(1); } code[cx].f = x; code[cx].l = y; code[cx].a = z; cx = cx + 1; } void test(unsigned long s1, unsigned long s2, long n) { if (!(sym & s1)) { error(n); s1 = s1 | s2; while(!(sym & s1)) { getsym(); } } } void enter(enum object k) // enter object into table { tx = tx + 1; strcpy(table[tx].name, id); table[tx].kind = k; switch(k) { case constant: if(num > amax) { error(31); num = 0; } table[tx].val = num; break; case variable: table[tx].level = lev; table[tx].addr = dx; dx = dx + 1; break; case proc: table[tx].level = lev; break; } } long position(char* id) // find identifier id in table { long i; strcpy(table[0].name, id); i=tx; while(strcmp(table[i].name, id) != 0) { i = i - 1; } return i; } void constdeclaration() { if(sym == ident) { getsym(); if(sym == eql || sym == becomes) { if(sym == becomes) { error(1); } getsym(); if(sym == number) { enter(constant); getsym(); } else { error(2); } } else { error(3); } } else { error(4); } } void vardeclaration() { if(sym == ident) { enter(variable); getsym(); } else { error(4); } } void listcode(long cx0) // list code generated for this block { long i; for(i=cx0; i<=cx-1; i++) { printf("%10d%5s%3d%5d\n", i, mnemonic[code[i].f], code[i].l, code[i].a); } } void expression(unsigned long); void factor(unsigned long fsys) { long i; test(facbegsys, fsys, 24); while(sym & facbegsys) { if(sym == ident) { i = position(id); if(i==0) { error(11); } else { switch(table[i].kind) { case constant: gen(lit, 0, table[i].val); break; case variable: gen(lod, lev-table[i].level, table[i].addr); break; case proc: error(21); break; } } getsym(); } else if(sym == number) { if(num>amax) { error(31); num=0; } gen(lit,0,num); getsym(); } else if(sym == lparen) { getsym(); expression(rparen|fsys); if(sym==rparen) { getsym(); } else { error(22); } } test(fsys,lparen,23); } } void term(unsigned long fsys) { unsigned long mulop; factor(fsys|times|slash); while(sym==times || sym==slash) { mulop = sym; getsym(); factor(fsys|times|slash); if(mulop == times) { gen(opr,0,4); } else{ gen(opr,0,5); } } } void expression(unsigned long fsys) { unsigned long addop; if(sym==plus || sym==minus) { addop=sym; getsym(); term(fsys|plus|minus); if(addop==minus) { gen(opr,0,1); } } else { term(fsys|plus|minus); } while(sym==plus || sym==minus) { addop=sym; getsym(); term(fsys|plus|minus); if(addop==plus) { gen(opr,0,2); } else { gen(opr,0,3); } } } void condition(unsigned long fsys) { unsigned long relop; if(sym==oddsym) { getsym(); expression(fsys); gen(opr, 0, 6); } else { expression(fsys|eql|neq|lss|gtr|leq|geq); if(!(sym&(eql|neq|lss|gtr|leq|geq))) { error(20); } else { relop=sym; getsym(); expression(fsys); switch(relop) { case eql: gen(opr, 0, 8); break; case neq: gen(opr, 0, 9); break; case lss: gen(opr, 0, 10); break; case geq: gen(opr, 0, 11); break; case gtr: gen(opr, 0, 12); break; case leq: gen(opr, 0, 13); break; } } } } void statement(unsigned long fsys) { long i,cx1,cx2; if(sym==ident) { i=position(id); if(i==0) { error(11); } else if(table[i].kind!=variable) // assignment to non-variable { error(12); i=0; } getsym(); if(sym==becomes) { getsym(); } else { error(13); } expression(fsys); if(i!=0) { gen(sto,lev-table[i].level,table[i].addr); } } else if(sym==callsym) { getsym(); if(sym!=ident) { error(14); } else { i=position(id); if(i==0) { error(11); } else if(table[i].kind==proc) { gen(cal,lev-table[i].level,table[i].addr); } else { error(15); } getsym(); } } else if(sym==ifsym) { getsym(); condition(fsys|thensym|dosym); if(sym==thensym) { getsym(); } else { error(16); } cx1=cx; gen(jpc,0,0); statement(fsys); code[cx1].a=cx; } else if(sym==beginsym) { getsym(); statement(fsys|semicolon|endsym); while(sym==semicolon||(sym&statbegsys)) { if(sym==semicolon) { getsym(); } else { error(10); } statement(fsys|semicolon|endsym); } if(sym==endsym) { getsym(); } else { error(17); } } else if(sym==whilesym) { cx1=cx; getsym(); condition(fsys|dosym); cx2=cx; gen(jpc,0,0); if(sym==dosym) { getsym(); } else { error(18); } statement(fsys); gen(jmp,0,cx1); code[cx2].a=cx; } test(fsys,0,19); } void block(unsigned long fsys) { long tx0; // initial table index long cx0; // initial code index long tx1; // save current table index before processing nested procedures long dx1; // save data allocation index dx=3; tx0=tx; table[tx].addr=cx; gen(jmp,0,0); if(lev>levmax) { error(32); } do { if(sym==constsym) { getsym(); do { constdeclaration(); while(sym==comma) { getsym(); constdeclaration(); } if(sym==semicolon) { getsym(); } else { error(5); } } while(sym==ident); } if(sym==varsym) { getsym(); do { vardeclaration(); while(sym==comma) { getsym(); vardeclaration(); } if(sym==semicolon) { getsym(); } else { error(5); } } while(sym==ident); } while(sym==procsym) { getsym(); if(sym==ident) { enter(proc); getsym(); } else { error(4); } if(sym==semicolon) { getsym(); } else { error(5); } lev=lev+1; tx1=tx; dx1=dx; block(fsys|semicolon); lev=lev-1; tx=tx1; dx=dx1; if(sym==semicolon) { getsym(); test(statbegsys|ident|procsym,fsys,6); } else { error(5); } } test(statbegsys|ident,declbegsys,7); } while(sym&declbegsys); code[table[tx0].addr].a=cx; table[tx0].addr=cx; // start addr of code cx0=cx; gen(Int,0,dx); statement(fsys|semicolon|endsym); gen(opr,0,0); // return test(fsys,0,8); listcode(cx0); } long base(long b, long l) { long b1; b1=b; while (l>0) // find base l levels down { b1=s[b1]; l=l-1; } return b1; } void interpret() { long p,b,t; // program-, base-, topstack-registers instruction i; // instruction register printf("start PL/0\n"); t=0; b=1; p=0; s[1]=0; s[2]=0; s[3]=0; do { i=code[p]; p=p+1; switch(i.f) { case lit: t=t+1; s[t]=i.a; break; case opr: switch(i.a) // operator { case 0: // return t=b-1; p=s[t+3]; b=s[t+2]; break; case 1: s[t]=-s[t]; break; case 2: t=t-1; s[t]=s[t]+s[t+1]; break; case 3: t=t-1; s[t]=s[t]-s[t+1]; break; case 4: t=t-1; s[t]=s[t]*s[t+1]; break; case 5: t=t-1; s[t]=s[t]/s[t+1]; break; case 6: s[t]=s[t]%2; break; case 8: t=t-1; s[t]=(s[t]==s[t+1]); break; case 9: t=t-1; s[t]=(s[t]!=s[t+1]); break; case 10: t=t-1; s[t]=(s[t]<s[t+1]); break; case 11: t=t-1; s[t]=(s[t]>=s[t+1]); break; case 12: t=t-1; s[t]=(s[t]>s[t+1]); break; case 13: t=t-1; s[t]=(s[t]<=s[t+1]); } break; case lod: t=t+1; s[t]=s[base(b,i.l)+i.a]; break; case sto: s[base(b,i.l)+i.a]=s[t]; printf("%10d\n", s[t]); t=t-1; break; case cal: // generate new block mark s[t+1]=base(b,i.l); s[t+2]=b; s[t+3]=p; b=t+1; p=i.a; break; case Int: t=t+i.a; break; case jmp: p=i.a; break; case jpc: if(s[t]==0) { p=i.a; } t=t-1; } } while(p!=0); printf("end PL/0\n"); } int main() { long i; for(i=0; i<256; i++) { ssym[i]=nul; } strcpy(word[0], "begin "); strcpy(word[1], "call "); strcpy(word[2], "const "); strcpy(word[3], "do "); strcpy(word[4], "end "); strcpy(word[5], "if "); strcpy(word[6], "odd "); strcpy(word[7], "procedure "); strcpy(word[8], "then "); strcpy(word[9], "var "); strcpy(word[10], "while "); wsym[0]=beginsym; wsym[1]=callsym; wsym[2]=constsym; wsym[3]=dosym; wsym[4]=endsym; wsym[5]=ifsym; wsym[6]=oddsym; wsym[7]=procsym; wsym[8]=thensym; wsym[9]=varsym; wsym[10]=whilesym; ssym['+']=plus; ssym['-']=minus; ssym['*']=times; ssym['/']=slash; ssym['(']=lparen; ssym[')']=rparen; ssym['=']=eql; ssym[',']=comma; ssym['.']=period; ssym[';']=semicolon; strcpy(mnemonic[lit],"LIT"); strcpy(mnemonic[opr],"OPR"); strcpy(mnemonic[lod],"LOD"); strcpy(mnemonic[sto],"STO"); strcpy(mnemonic[cal],"CAL"); strcpy(mnemonic[Int],"INT"); strcpy(mnemonic[jmp],"JMP"); strcpy(mnemonic[jpc],"JPC"); declbegsys=constsym|varsym|procsym; statbegsys=beginsym|callsym|ifsym|whilesym; facbegsys=ident|number|lparen; printf("please input source program file name: "); scanf("%s",infilename); printf("\n"); if((infile=fopen(infilename,"r"))==NULL) { printf("File %s can't be opened.\n", infilename); exit(1); } err=0; cc=0; cx=0; ll=0; ch=' '; kk=al; getsym(); lev=0; tx=0; block(declbegsys|statbegsys|period); if(sym!=period) { error(9); } if(err==0) { // interpret(); } else { printf("errors in PL/0 program\n"); } fclose(infile); return (0); }
06-05
<think>好的,我现在需要帮助用户解决Verilog中出现的'expecting a statement'错误。首先,我得回忆一下常见的Verilog语法错误,然后针对这个特定的错误进行分析。 用户提到的错误信息是“expecting a statement”,通常这意味着在代码的某个位置,解析器预期找到一条合法的语句,但实际遇到了不符合语法规则的内容。接下来,我需要考虑哪些情况会导致这种错误。 首先,可能的情况是语句缺少分号。Verilog中每个语句必须以分号结尾,比如在always块内部,赋值语句后面如果没有分号,就会报错。比如: always @(*) begin a = b + c // 缺少分号 end 这种情况下,解析器在遇到end之前会发现缺少分号,从而提示需要语句。所以用户需要检查每个语句是否都正确结束。 其次,结构块(如always、initial、if-else、case等)的语法错误。比如,在if语句中忘记添加begin-end块,尤其是当有多个语句时。例如: if (condition) a = b; // 正确,单条语句不需要begin-end c = d; // 错误,这里会被认为是if之外的部分 但更可能的情况是用户可能错误地使用了括号或者结构。例如,if语句后面没有跟语句,或者begin拼写错误。 第三个可能的原因是运算符或表达式错误,比如在赋值语句中使用了不正确的运算符,或者在条件表达式中缺少括号。例如: assign a = b + c * d; // 正确 assign a = b + c * ; // 错误,缺少操作数 这种情况下,解析器可能在遇到*时发现后面没有操作数,导致语句不完整。 第四个可能的问题是关键字拼写错误,比如把always写成alway,或者begin写成begn。这会导致解析器无法识别结构,从而提示需要语句。 第五,检查过程块中的非阻塞赋值是否正确使用。虽然这通常不会直接导致'expecting a statement',但有时候格式错误可能连带引发其他问题。不过这可能属于其他错误类型,比如在组合逻辑中使用非阻塞赋值可能引发警告,但不会直接导致这个错误。 接下来,我需要考虑用户可能的代码示例,以便给出具体的错误和修正案例。例如,用户可能在always块中漏掉了分号: always @(posedge clk) begin a <= b + c // 缺少分号 d <= e end 这里,b + c后面缺少分号,解析器在下一行的d处会报错,因为它预期在a <= b + c后面有一个分号,作为语句的结束。 另外,用户可能在case语句中漏掉了endcase,或者if语句缺少配对: case (sel) 2'b00: a = b; 2'b01: a = c; default: a = d; // 缺少endcase 这会使得解析器在遇到后续代码时,认为case结构未正确结束,从而报错。 还有可能是在模块实例化时,端口连接没有正确使用逗号或括号,例如: module my_module (input a, output b); endmodule module top; my_module inst (a, b // 缺少右括号或分号 endmodule 这里,实例化语句缺少右括号或分号,导致解析器预期有语句。 在给出解决方案时,需要分步骤说明,比如检查分号、检查结构块是否正确闭合、验证表达式完整性等。同时,提供代码示例的错误和正确版本对比,帮助用户理解。 此外,用户的问题中提到参考引用中的例子是关于项目路径中有空格导致的错误,但当前问题与语法相关,所以可能不需要直接引用,但需要确保回答不依赖该引用,而是专注于语法错误本身。 最后,生成相关问题时要考虑用户可能的后续疑问,比如如何处理其他常见错误,或Verilog的调试工具使用等。</think>### Verilog 'expecting a statement' 错误原因及解决方案 在Verilog开发中,`expecting a statement` 错误通常由语法结构缺失或格式错误引起。以下是常见原因及对应解决方法: --- #### 1. **语句缺少分号** Verilog要求每个语句以分号 `;` 结尾。若在 `always`、`initial` 或 `assign` 等块中遗漏分号,编译器会报此错误。 **错误示例:** ```verilog always @(*) begin a = b + c // 缺少分号 end ``` **修正后:** ```verilog always @(*) begin a = b + c; // 添加分号 end ``` --- #### 2. **结构块语法错误** 在 `if-else`、`case`、`for` 等结构块中,若未正确闭合或格式错误,会触发此问题。 - **`if-else` 缺少 `begin-end`** 当 `if` 或 `else` 包含多条语句时,需用 `begin-end` 包裹。 **错误示例:** ```verilog if (condition) a = b; c = d; // 此句在 if 块外,导致逻辑错误 ``` **修正后:** ```verilog if (condition) begin a = b; c = d; end ``` - **`case` 缺少 `endcase`** `case` 语句必须以 `endcase` 结束。 **错误示例:** ```verilog case (sel) 2'b00: a = b; 2'b01: a = c; // 缺少 endcase ``` **修正后:** ```verilog case (sel) 2'b00: a = b; 2'b01: a = c; endcase ``` --- #### 3. **运算符或表达式不完整** 表达式未完整书写可能导致编译器无法解析。 **错误示例:** ```verilog assign a = b + ; // 缺少操作数 ``` **修正后:** ```verilog assign a = b + c; ``` --- #### 4. **关键字拼写错误** Verilog关键字拼写错误(如 `alway` 代替 `always`)会导致语法结构失效。 **错误示例:** ```verilog alway @(posedge clk) begin // 拼写错误 a <= b; end ``` **修正后:** ```verilog always @(posedge clk) begin a <= b; end ``` --- #### 5. **模块实例化错误** 实例化模块时,若端口连接格式错误(如缺少逗号或括号),也会引发此问题。 **错误示例:** ```verilog module sub_module (input a, output b); endmodule module top; sub_module inst (a, b // 缺少右括号 endmodule ``` **修正后:** ```verilog module top; sub_module inst (a, b); endmodule ``` --- ### 调试建议 1. **逐行检查分号**:确保所有语句以 `;` 结尾。 2. **验证结构闭合**:检查 `begin-end`、`case-endcase`、`if-else` 的配对。 3. **使用语法高亮工具**:如Vivado、VS Code等,辅助发现语法错误。 4. **简化代码**:通过注释隔离代码段,定位具体错误位置。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值