关于<:if>没有<c:else>解决方案

 
<c:if>没有<c:else>可以用<c:choose>来取代结构:
<c:choose>

   <c:when test="">    如果
   </c:when>
   
   <c:otherwise>  否则
   </c:otherwise>
</c:choose>

在同一个 <c:choose> 中,当所有 <c:when> 的条件都没有成立时,则执行 <c:otherwise> 的本体内容。

  语法

  <c:otherwise>

  本体内容

  </c:otherwise>

  属性

  无

  限制

  ·<c:otherwise> 必须在 <c:choose> 和 </c:choose>之间

  ·在同一个 <c:choose> 中时,<c:otherwise> 必须为最后一个标签

  说明

  在同一个 <c:choose> 中,假若所有 <c:when> 的test属性都不为true时,则执行 <c:otherwise> 的本体内容。

  范例

  笔者举一个典型的 <c:choose>、<c:when>和<c:otherwise>范例:

  <c:choose>

  <c:when test="${condition1}">

  condition1为true

  </c:when>

  <c:when test="${ condition2}">

  condition2为true

  </c:when>

  <c:otherwise>

  condition1和condition2都为false

  </c:otherwise>

  </c:choose>

  范例说明:当condition1为true时,会显示“condition1为true”;当condition1为false且condition2为true时,会显示“condition2为true”,如果两者都为false,则会显示“condition1和condition2都为false”。

  注意

  假若condition1和condition2两者都为true时,此时只会显示"condition1为true",这是因为在同一个<c:choose>下,当有好几个<c:when>都符合条件时,只能有一个<c:when>成立。


  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
为了将给定的文件汇编成所需的形式,我们需要编写一些规则。这些规则将告诉 Yacc 如何解析输入文件并生成汇编代码。以下是一个可能的解决方案: ``` %{ #include <stdio.h> #include <stdlib.h> #include <string.h> int lineno = 1; %} %token INT ID VOID IF ELSE RETURN WHILE NUMBER %% program: | program external_declaration ; external_declaration: declaration | function_definition ; declaration: INT ID ';' ; function_definition: type_specifier ID '(' parameter_list ')' compound_statement ; type_specifier: INT | VOID ; parameter_list: parameter_declaration | parameter_list ',' parameter_declaration ; parameter_declaration: type_specifier ID | type_specifier ID '[' ']' ; compound_statement: '{' '}' | '{' statement_list '}' ; statement_list: statement | statement_list statement ; statement: expression_statement | compound_statement | selection_statement | iteration_statement ; expression_statement: ';' | expression ';' ; selection_statement: IF '(' expression ')' statement | IF '(' expression ')' statement ELSE statement ; iteration_statement: WHILE '(' expression ')' statement ; expression: assignment_expression | expression ',' assignment_expression ; assignment_expression: equality_expression | unary_expression '=' assignment_expression ; equality_expression: relational_expression | equality_expression '==' relational_expression | equality_expression '!=' relational_expression ; relational_expression: additive_expression | relational_expression '<' additive_expression | relational_expression '>' additive_expression ; additive_expression: multiplicative_expression | additive_expression '+' multiplicative_expression | additive_expression '-' multiplicative_expression ; multiplicative_expression: unary_expression | multiplicative_expression '*' unary_expression | multiplicative_expression '/' unary_expression ; unary_expression: postfix_expression | '-' unary_expression ; postfix_expression: primary_expression | postfix_expression '[' expression ']' | postfix_expression '(' ')' | postfix_expression '(' argument_expression_list ')' ; primary_expression: ID | NUMBER | '(' expression ')' ; argument_expression_list: assignment_expression | argument_expression_list ',' assignment_expression ; %% int main() { yyparse(); return 0; } void yyerror(char *s) { fprintf(stderr, "line %d: %s\n", lineno, s); } int yywrap() { return 1; } ``` 这是一个非常基本的 Yacc 规则集,它定义了语言的各个部分。您需要将此代码保存到名为 `grammar.y` 的文件中。 接下来,我们需要创建一个名为 `lex.l` 的新文件,其中包含与语法规则匹配的词汇表。 ``` %{ #include "y.tab.h" %} %% "int" { return INT; } "void" { return VOID; } "if" { return IF; } "else" { return ELSE; } "return" { return RETURN; } "while" { return WHILE; } [0-9]+ { yylval.num = atoi(yytext); return NUMBER; } [a-zA-Z][a-zA-Z0-9]*{ yylval.str = strdup(yytext); return ID; } "[" { return '['; } "]" { return ']'; } "(" { return '('; } ")" { return ')'; } ";" { return ';'; } "{" { return '{'; } "}" { return '}'; } "+" { return '+'; } "-" { return '-'; } "*" { return '*'; } "/" { return '/'; } "==" { return EQ; } "!=" { return NE; } "<" { return '<'; } ">" { return '>'; } "=" { return '='; } "," { return ','; } [ \t\n] { /* ignore whitespace */ } . { printf("Unknown token: %s\n", yytext); } %% int main() { yyparse(); return 0; } void yyerror(char *s) { fprintf(stderr, "line %d: %s\n", yylineno, s); } int yywrap() { return 1; } ``` 请注意,此文件包含与 `y.tab.h` 匹配的标头文件,并且包含了一些正则表达式以匹配输入文件中的单词。您需要将此文件保存为 `lex.l`。同时,还需要生成一个头文件 `y.tab.h`,这可以通过在终端中执行以下命令来完成: ``` yacc -d grammar.y ``` 最后,我们需要生成一个可执行文件并测试它是否按预期工作。这可以通过在终端中执行以下命令来完成: ``` yacc -d grammar.y lex lex.l cc y.tab.c lex.yy.c -o compiler ./compiler < input.txt ``` 其中,`input.txt` 是您想要编译的输入文件。如果一切正常,您将会看到输出文件的内容显示在终端上。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值