You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Lex/Yacc开发Shell解释器时execvp()报无效选项问题求助

Troubleshooting the ls: invalid option -- Error in Your Lex/Yacc Shell

Hey there! Let's break down this tricky issue you're facing—this is a super common pitfall when building command-line parsers with Lex and Yacc, so we'll get to the bottom of it.

First, let's recap: your arg_list looks like it's storing the correct values (ls and -l), but when you execute the command, ls complains about an invalid empty option. Here are the most likely culprits:

1. Invisible/Extra Characters in Your Arguments

Even if your arg_list looks correct when printed, it might be hiding invisible characters like trailing spaces, newlines, or control characters that aren't obvious at first glance. For example:

  • If your Lex scanner isn't trimming whitespace properly, -l might actually be stored as -l\n (with a newline at the end)
  • Or a stray null byte could be inserted mid-string, making ls read only the part before it

How to check: Add debug code to print each argument's raw bytes. For example, in your parser code right before executing the command:

for (int i = 0; arg_list[i] != NULL; i++) {
    printf("Arg %d: '%s' | Hex bytes: ", i, arg_list[i]);
    for (char *c = arg_list[i]; *c != '\0'; c++) {
        printf("%02x ", (unsigned char)*c);
    }
    printf("\n");
}

If you see extra bytes (like 0a for newline) after -l, that's your problem. Adjust your Lex rules to strip trailing whitespace or exclude control characters from tokens.

2. Missing Null Termination for Strings

C relies on \0 to mark the end of a string. If your Lex/Yacc code isn't properly null-terminating the strings in arg_list, execvp (or whichever exec function you're using) will read garbage data beyond the intended argument. For example:

  • If you're copying characters into a buffer manually without adding \0 at the end
  • Or using strncpy without explicitly setting the last byte to \0

Fix: Always use strdup(yytext) in your Lex actions to create null-terminated copies of tokens, instead of assigning raw pointers to yytext (which gets overwritten by the next token). Example:

"-"[a-zA-Z]+ { yylval.str = strdup(yytext); return OPTION; }
[a-zA-Z0-9]+ { yylval.str = strdup(yytext); return WORD; }

3. Incorrect Argument Array Structure for exec

The exec family of functions expects the argument array to:

  • Start with the command name (e.g., "ls")
  • Follow with each argument (e.g., "-l")
  • End with a NULL pointer to mark the end of the list

If you're missing that final NULL, or accidentally adding an empty string ("") instead, ls will interpret the garbage data (or empty string) as an invalid option. For example, if your array is ["ls", "-l", ""] instead of ["ls", "-l", NULL], ls will try to parse the empty string as an option, leading to the error you see.

Check: Double-check how you're building arg_list. Make sure after adding all arguments, you append a NULL pointer. Example:

// After adding all args to arg_list
arg_list[arg_count] = NULL;

4. Misconfigured Lex Token Rules

If your Lex rules split options into separate tokens (like a standalone - and a separate l), your parser might be passing them as two separate arguments to ls. For example:

  • If your rules are:
    "-" { return DASH; }
    [a-zA-Z]+ { yylval.str = strdup(yytext); return WORD; }
    

Then -l gets split into DASH and WORD tokens, and your parser might add "-" and "l" as separate arguments to arg_list. ls will see - as an invalid option (since it has no following character).

Fix: Adjust your Lex rules to capture full options as a single token. Use a rule like:

"-"[a-zA-Z]+ { yylval.str = strdup(yytext); return OPTION; }

This way, -l is treated as one token, and your parser will add it as a single argument to arg_list.

Start with checking the argument array structure and null termination first—those are the most frequent causes of this exact error. Then move on to inspecting token rules and invisible characters.

内容的提问来源于stack exchange,提问作者Vedant Tripathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:31:13